Basic definition and concept of a class (extension 2) about inheritance 1. Inheritance is a mechanism for obtaining one or more derived classes from a base class
2. a class that inherits from another class is called a subclass of this class. this relationship is generally described by a parent-child relationship. the subclass inherits the special effects of the parent class. these features are composed of attributes and methods.
(A parent class can also be called a superclass, a base class, or a subclass called a parent class extension)
3. the derived class can extend and modify the function of the parent class.
< ?php class ShopProduct{ function getSummaryLine() { echo 'i am father\'s getSummaryLine function '."\n"; } } class Cdproduct extends ShopProduct{ function getSummaryLine() { echo 'i am son\'s getSummaryLine function'."\n"; } } $product1 = new ShopProduct(); $product2 = new Cdproduct(); $product1->getSummaryLine(); echo "-------------\n"; $product2->getSummaryLine();?>
Output result:
I am father's getSummaryLine function ------------- I am son's getSummaryLine function // changed
4. to reference a class rather than an object method, use: instead of->. 5. when defining a constructor in a subclass, you must pass parameters to the constructor of the parent class. Otherwise, you may obtain an incomplete object.
Use parent ::__ construct ()
<? Php class ShopProduct {public $ title; function _ construct ($ title) {// Constructor of the parent class, here you need to input a parameter $ title // ---- step 4 $ this-> title = $ title; echo 'I am father \'s construct and title is '. $ title. "\ n";} function getSummaryLine () {echo 'I am father \'s getSummaryLine function '. "\ n" ;}} class Cdproduct extends ShopProduct {public $ sonTitle; function _ construct ($ title, $ sonTitle) {// Constructor, add a new parameter $ sonTitle // ---- Step 2 parent ::__ construct ($ title); // The constructor of the parent class is called here, because the constructor of the parent class has only one parameter $ title, therefore, you only need to input one // ---- step 3 $ this-> sonTitle = $ sonTitle; // This is an attribute to be added by the subclass's own constructor. // The above rows add up to the constructor of the parent class, add a subclass attribute ($ sonTitle) on the basis of the construction method of the parent class (obtain $ title of the parent class) echo 'I am son \'s construct and title is '. sonTitle. "\ n";} function getSummaryLine () {echo 'I am son \'s getSummaryLine function '. "\ n" ;}}$ product1 = new ShopProduct ('t Tttitle'); echo "------------- \ n"; $ product2 = new Cdproduct ('tttitle2222 ', 'dassada '); // ---- step 1 (if you only look at the instantiation process of the Cdproduct subclass, it is step 4) echo "------------- \ n"; $ product1-> getSummaryLine (); echo "------------- \ n"; $ product2-> getSummaryLine ();?>
The output result is
i am father's construct and title isTTTTitle-------------i am father's construct and title isTTTitle2222i am son's construct and title issonTitle-------------i am father's getSummaryLine function -------------i am son's getSummaryLine function
Each subclass calls the constructor of the parent class before setting its own attributes. The parent class only needs to know its own data. We should avoid telling the parent class any message about the subclass. this is an empirical rule. (In fact, in object-oriented development, "focus on specific tasks and ignore external context ")
6. the public, private, and protected-visibility keywords allow us to publish only the parts required by customers in the class, which sets a clear interface for the object.
Public is all open, private is only called for the current class, and protected can be called for the current class and Subclass. the security level is public <protected <private (we tend to strictly control the accessibility)
6.1 access method
Strictly control the accessibility based on security considerations, so there is a way to achieve
Class ShopProduct {private $ title; // because private is set, public function getTitle () {// cannot be accessed without the current class, but we need to access this attribute again, therefore, a public method is set for access. we can add some filtering and judgment in this method to improve further accessibility. Return $ this-> title;} public function setTitle () {// this is the set method. of course, you cannot simply set it. you need to add related filtering operations. Return $ this-> title = 'hahhah ';}}