PHP Object-oriented Programming, PHP object-oriented
The basic principles of object-oriented programming:
- Single duty: A class, only need to do a good thing
- Open closure: A class that should be extensible, not modifiable
- Dependency Inversion: A class that should not be strongly dependent on another class. Each class corresponds to a second class that is replaceable
- Configure: Use configuration as much as possible instead of hard-coded
- Interface-oriented programming: Just care about the interface, no need to care about implementation
1. Set the properties of the class
class shopproduct { public$title = ' default product '; Public $producterMainName = ' main name '; Public $producterFirstName = ' First name '; Public $price = 0;} $product 1 New shopproduct (); // Setting Properties $product 1->title = "My Antonia"; $product 1->producterfirstname = "Cather"; $product 1->productermainname = "Willa"; $product 1->price = 5.99; // Access Echo ' Author: '. $product 1->producterfirstname. ' '. $product 1->productermainname;
There are a number of problems with setting property values in these ways:
First: PHP allows properties to be set dynamically, and does not get a warning if you misspell or forget the property name. For example, mistakenly put
$product 1->productermainname = "Willa";
Writing
$product 1->productersecondname = "Willa";
, when we export the author's name, there will be unexpected results.
Second: The class is too loose, we don't have to force the title, price, or product name, and the client code can determine that these attributes exist, but it may or may not be the default, ideally we want to instantiate the Shopproduct object when we set a meaningful property value.
Third: Have to do something often, such as the need to complete the full output of the author's name, it is necessary to reuse
Echo ' Author: '. $product 1->producterfirstname. ' '. $product 1->productermainname;
http://www.bkjia.com/PHPjc/1108855.html www.bkjia.com true http://www.bkjia.com/PHPjc/1108855.html techarticle PHP Object-oriented programming, PHP Object-oriented object-oriented programming basic principles: Single duty: A class, only need to do a good thing open closed: a class, should be extensible, and ...