在PHP对象中的对象? [英] Objects within objects in PHP?

查看:108
本文介绍了在PHP对象中的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新在PHP中OOP,我想什么是下面的情况下,最好的做法一些建议:
说,我自己的商店销售不同类型的东西,如:书籍,电影,和盘子。这些不同的producttypes各自具有不同的属性(盘子有颜色和材质,书有一位作家和电影有演员和导演)。我可以创建每个类型的产品我卖的一类,但后来我需要重新code时,我也想卖猫。相反,我可以创建一个类产品,也有一些属性所有产品都有(如名​​称,价格,酒吧code)。现在,什么是存储producttype特定属性的最佳方式,是其他类producttype或只是一个简单的阵列产品中?

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 );
    }
}

在上面的例子中,有可能还轻松存储(例如)的数据类型和不同特性的不同productTypes具有必需字段。一个别的路可走它,是通过简单地使用数组(见下文),但你需要的数据类型存储在其他阵列或阵列会变得非常大,你总是返回整个数组。

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 );
    }
}

我想,这最后的选择破坏对象的目的,但我不知道。我看着延长了一流的产品的可能性,但我仍然需要每productType一个特定的类。如果有我俯瞰其他选项,请让我知道。

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.

编辑:我现在看到,我也没有问我的问题很清楚,而且在事实上,我问错了问题。为此我问了一个新问题:与配置属性 PHP对象类

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: PHP object class with configurable properties?

推荐答案

考虑与工作接口,一个接口是一个合同而实现类必须兑现。

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
}

现在在您的集成code,当你想确保你的对象确实是一个有效的产品,你可以使用TypeHinting以及实例检查:

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){}

上面的方法将接受任何类型的产品实现,因此要保证下实现的关键getName和用getPrice方法的对象。

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 !}

我们真的只是皮毛的PHP面向对象能力的力量的表面,但hopefuly这会给你在正确的方向指针。

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.

这篇关于在PHP对象中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆