Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7870693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:41:30+00:00 2026-06-03T01:41:30+00:00

Let’s says I have 3 items : a keyboard, a tshirt and a bottle

  • 0

Let’s says I have 3 items : a keyboard, a tshirt and a bottle of cola.

$keyboard = new Item("Keyboard");
echo $keyboard->getPrice(); // return 50;

$tshirt = new Item("Tshirt");
echo $tshirt->getPrice(); // return 20;

$cola = new Item("Cola");
echo $cola->getPrice(); // return 0 or 2 whether the bottle is empty or not.

What is the best practice to get the Price of the bottle of cola ?

I started by creating 2 classes :

Class Item {
    $this->price;

    function __construct($name) {
    // ...
    }

    public function getPrice() {
        return $this->price;
    }
}

Class Bottle extends Item {
    $this->empty;

    function __construct($name) {
    // get from database the value of $this->empty
    }
    public function getPrice() {
        if($this->empty)
            return 0;
        else 
            return $this->price;
    }
}

But now I’m wondering ; when I use : $cola = new Item("Cola");, I’m instantiating an Item object and not a Bottle object, because I don’t know yet if it’s a “normal” item or a bottle.

Should I instead instantiating a Bottle object and research for another logic in my application ? Or is there a way to “recreate” the item object and transforms it as a bottle ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:41:31+00:00Added an answer on June 3, 2026 at 1:41 am

    This is a perfect example of when to use the Factory Pattern.

    For your code, you could do something like this.

    class ItemFactory {
        // we don't need a constructor since we'll probably never have a need
        // to instantiate it.
        static function getItem($item){
            if ($item == "Coke") {
                return new Bottle($item);
            } else if ( /* some more of your items here */){
                /*code to return object extending item*/
            } else { 
                // We don't have a definition for it, so just return a generic item.
                return new Item($item);
            }
        }
    }
    

    You can use it like $item = ItemFactory::getItem($yourvar)

    The Factory Pattern is useful when you have a lot of objects that have the same base (or parent) class, and you need to determine what class they are at runtime.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option
Let's say I have the following classes : public class MyProductCode { private String
Let's say I have a due date and a reminder timespan . How do
Let assume we have two activities. A - main activity, that is home launcher
Let's say you have a method that expects a numerical value as an argument.
Let's say I have a bunch of links that share a click event: <a
Let's suppose I have an XML file like this: <?xml version=1.0 encoding=ISO-8859-1?> <MIDIFile> <Event>
Let's say we have 3 classes: A, B and C. Each class has the
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.