简体   繁体   中英

Objects within objects in PHP?

i'm fairly new to OOP in PHP and i'd like some advice on what's the best practice for the case below: Say, i own a store that sells different kind of things, eg. books, movies, and dinner plates. These different producttypes each have different properties (dinner plates have a color and material, books have an author and movies have actors and a director). I could create a class for each of the types of products i sell, but then i need to recode when i also want to sell cats. Instead, i could create a class "product", that has some properties that all products have (eg. name, price, barcode). Now, what's the best way to store the producttype specific properties, an instance of an other class "producttype" or just a simple array within product?

Hopes below example explain it a little better:

class product {
    private $s_name;
    private $i_price;
    private $s_barcode;
    private $o_properties;

    function __construct ( $name, $productType ) {
        $this->s_name = $name;
        $this->i_price = getPriceFromDb ( $name );
        $this->s_barcode = getBarcodeFromDb ( $name );
        $this->o_properties = new productType ( $this->s_name, $productType )
    }
}

class productType {
    private $a_propertieValues;
    private $a_datatypes;
    private $a_requiredFields;

    function __construct ( $name, $productType ) {
        $this->a_datatypes = getDatatypesFromDb ( $productType );
        $this->a_propertieValues = getPropertieValuesFromDb ( $name );
        $this->a_requiredFields = getRequiredFieldsFromDb ( $productType );
    }
}

In the example above, it's possible to also easily store (for instance) the datatype and required fields of the different properties the different productTypes have. An other way to go at it, is by simply using an array (see below), but then you need to store the datatypes in an other array or your array will become very large and you always return the entire array.

class product {
    private $s_name;
    private $i_price;
    private $s_barcode;
    private $a_properties;

    function __construct ( $name, $productType ) {
        $this->s_name = $name;
        $this->i_price = getPriceFromDb ( $name );
        $this->s_barcode = getBarcodeFromDb ( $name );
        $this->a_properties = getOtherPropertiesFromDb ( $productType, $name );
    }
}

I think this last option undermines the purpose of objects, but i'm not sure. I've looked into the possibility of extending the class product, but then i still need a specific class per productType. If there are other options i'm overlooking, please let me know.

I hope i've explained this in a way you can understand.

Edit: I now see that i didn't ask my question very clear and that, in fact, i asked the wrong question. I therefor asked a new question: https://stackoverflow.com/questions/27138816/

Consider working with Interfaces, an interface is a contract which implementing classes must honour.

Interface Product{
public function getName();
public function getPrice();
}

class Cat implements Product{
//must implement getName and getPrice
}

Now in your integration code when you want to make sure you're object is indeed a valid Product you can use TypeHinting as well as instance checking:

public function addToCart(Product $product){}

above method will accept any type of object which implements Product and therefore is garanteed to implement the crucial getName and getPrice methods.

if you want to check what type of product you are dealing with:

if($product instanceof Cat){//this is a Cat product !}

We are really just scratching the surface of the power of PHP OOP capabilities but hopefuly this will give you a pointer in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM