Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php oo-如何正确操作?_Php_Oop - Fatal编程技术网

php oo-如何正确操作?

php oo-如何正确操作?,php,oop,Php,Oop,我第一次使用OO创建了一个网站,我正在努力找到正确的方法。 我不喜欢所有事情都在一个类中完成的类,所以我将实体类和数据库类分开。 例如: 我觉得这门课不太合乎逻辑。 -为什么产品应该包含数据库实例 -$product->get_products()我认为根本不符合逻辑 因此,我根据以下结构创建了一个单独的类: Table: Products Fields: productID,productName,categoryID Table: ProductCategory Fields: categ

我第一次使用OO创建了一个网站,我正在努力找到正确的方法。 我不喜欢所有事情都在一个类中完成的类,所以我将实体类和数据库类分开。 例如: 我觉得这门课不太合乎逻辑。 -为什么产品应该包含数据库实例 -$product->get_products()我认为根本不符合逻辑

因此,我根据以下结构创建了一个单独的类:

Table: Products
Fields: productID,productName,categoryID

Table: ProductCategory
Fields: categoryID,category

Classes:
Product
-productID
-productName
-category (object)

DataProduct
-insertProduct($product)
    insert query...
-deleteProduct($product)
    delete query...
-getProduct($id)
    select data from table ... (join between product and category to get all the fields)
    $category = new Category($categoryID,$categoryname)
    $product = new Product($productID,$productName);
    $product->setCategory($category);
    return $product;

ProductCategory
-categoryID
-category

DataProductCategory
...
但现在我有一些问题要问我是否处理得当

问题1 例如,要删除一个产品,我可以执行两个不同的脚本,我应该使用哪一个

http://www.mywebsite.com/books/1/
$id = 1 (1 retrieved from the url)
$DataProduct = new DataProduct();
$DataProduct->deleteProduct($id);
OR
$product = new Product();
$product->setId($id);
$DataProduct = new DataProduct();
$DataProduct->deleteProduct($product);
问题2 从数据库检索产品时,我创建了一个新的产品对象,并将该类别作为对象添加到产品中。 假设您有一个adminpage,希望在其中添加新产品,并且有一个带有categorieID的选项列表:

<input type="text" name="productname">
<select name="categories">
<option name="cat1" value="1">
<option name="cat2" value="2">
</select>
只需获取$product->categoryID并将其与$product->productName一起插入数据库

或者我应该:

$product = new Product();
$product->setProductName("test");
$dataCategory = new DataCategory();
$dataProduct = new DataProduct();
$category = $dataCategory->getCategory(); (which retrieves the categoryID and name, creates the object and returns it)
$product->setCategory($category);
$dataProduct->insertProduct($product);
保存并从对象中检索类别的id


这两种方法都有效,但我有点被哪种方法所困扰。

问题1

我建议使用
DataProduct
方法。尽管它与
产品
更相关,但您已经在使用
数据产品
进行类似的操作,例如插入和更新。我想说的是,你也需要坚持使用相同的对象来删除

问题2

尽管我的感觉是你应该坚持第一种方法,因为它更整洁,但第二种方法是OOP正确的。您仅将属性设置为
产品
类,因此如果您在
产品
类中通过
ID
获取
类别
对象,则会很麻烦。相反,您希望设置整个对象



OOP是一个很难的理论问题,但你做得很好。我知道很难判断你自己的OOP,因为你经常从编程的角度来看它,而不是从OOP的角度看它,这使得它变得比现在更难。

属于你所描述的功能都与你从数据库检索东西的方式有关。有一些现有的模式可以实现这一点——通常称为“对象关系映射”,或ORM。这里有一个广泛的讨论:。我已经读过了——即使你决定不使用它,它也会为你自己的设计提供背景。另请参阅,以获得概述和10种不同ORM模式的链接。
$product = new Product();
$product->setProductName("test");
$dataCategory = new DataCategory();
$dataProduct = new DataProduct();
$category = $dataCategory->getCategory(); (which retrieves the categoryID and name, creates the object and returns it)
$product->setCategory($category);
$dataProduct->insertProduct($product);