This is the 8th day of my participation in the More text Challenge. For details, see more text Challenge
When the first two sections introduced the components of a class, they mentioned two things: visibility and inheritance.
Member variables, member methods, and class constants can all be inherited, and visibility determines how well they are inherited. So we talk about visibility and inheritance together.
Basic knowledge of
Extend. Inheritance is the mechanism by which subclasses automatically share properties and methods of their parent class. This is a relationship between classes.
Parent class a class that is inherited by other classes can be called a base class.
Subclass − A class that inherits from another class is called a subclass, or a derived class, which is an extension of the parent class.
There are a few basic rules about inheritance:
-
PHP does not support multiple inheritance. A class can only inherit one parent class, but a parent class can have multiple subclasses.
-
A child class can also be a parent of another class. Multilevel inheritance is supported in PHP.
-
The parent class must be declared before the child class. This rule applies when classes inherit from other classes and interfaces.
The following example shows the relationship between a child class and its parent class and multi-level inheritance:
class A {} class B extends A {} class C extends B {} class D extends B {} $someObj = new A(); $someOtherObj = new B(); $anotherObj = new C(); $lastObj = new D(); //B is A subclass of A, C and D are both subclasses of B, so C and D are also subclasses of ACopy the code
Let’s review the Visibility restriction again:
-
Public is the data information that can be exposed, there is no need to hide, can be called anywhere in the program by other classes and objects, subclasses can inherit and use all the public members of the parent class
-
Private member variables and methods modified by the private keyword can only be called and modified inside the class they belong to. They cannot be accessed outside the class and cannot be called or modified in a subclass
-
A protected, decorated member variable or method that can only be accessed internally by itself and by its subclasses and superclasses.
-
If no visibility keyword is written, the system defaults to public
Then we can tell that the only properties and methods that a child can inherit are those of its parent class that are not private. Let’s look at this example:
class Woshinibaba { private $private='fatherprivate'; $protected='fatherprotected'; $public=' fatherPublic '; $public=' fatherPublic '; Private function fatherprivate() {echo $this->private; // Access private properties; echo $this->protected.'<br>'; // Access protected attributes; echo $this->public.'<br>'; // Access public properties; $this->private; $this->private; $this->private; echo $this->protected.'<br>'; echo $this->public.'<br>'; } public function fatherpublic() {echo $this->private; echo $this->protected.'<br>'; echo $this->public.'<br>'; }} class Test7 extends Woshinibaba {public function childpublic() {echo $this->public; $this->protected; $this->protected; $this->private; $this->private; } function childtest() {parent::fatherprotected(); Parent :: fatherPrivate (); parent::fatherprivate(); $child= new Test7();}} $child= new Test7(); // Create a subclass object; $child->fatherprivate(); // The parent private method cannot be accessed outside the class; $child->fatherprotected(); // The parent protected method cannot be accessed outside the class; $child->fatherpublic(): $child->fatherpublic(): $child-> fatherprivate fatherprotected fatherpublic(); $child->childpublic(); // output fatherpublic fatherprotected, with Undefined property:$private, because you can't access the parent private variable from subclasses; $child->childtest(); // Only fatherprotected() works; echo $child->public; Fatherpublic; public properties can be accessed anywhere; echo $child->protected; // Error: Cannot access protected property; echo $child->private; // Failed to access the parent private property;Copy the code
From the above example, there are two more key points:
- A child can use the parent:: pronoun to access a protected method of the parent class. Parent refers to the parent class of the class where the method is located.
- The inner of a class refers to the code within class{}, and the outer of a class refers to the code outside of it. Protected methods cannot be called outside of the class;
Rewrite the override
In addition to inheriting non-private members of the parent class, subclasses can also implement different functions by overwriting members of the parent class with the same name. This is called overwriting. Subclasses can override the properties and methods of the parent class or add properties and methods that belong only to them.
Methods to rewrite
Let’s start with an example of an all-in-one member method override:
Public function foo(int $a, $b, $b) $c='test') {} final public function bar() {echo 'test'; }} class Extend1 extends Base1 {/ / subclasses can change the required parameters of the parent class to optional parameters, and add the optional parameters, modifying parameters default values, you can delete the parameter type, and formulate the function return type foo ($a, $b = 10, $c='done', array $d=[]): float { return $DDD=$a*$b; } function milk() {parent::foo(); // Subclasses can still call superclass methods; }} class Extend2 extends Base1;}} class Extend2 extends Base1; {function foo(float $a=5, $b, $c='test') {}} class Extend3 extends Base1 {function foo(int $a, $b, $c) {} Function bar() // Failed to override parent final method; { echo 'done'; }} class Extend4 extends Base1 {} class Extend4 extends Base1 {} class Extend4 extends Base1 {} $c=10) {}} class Extend5 extends Base1 {} public function foo(int $a, $b, $b, $c) {}} class Extend5 extends Base1 {} $c=10) {} }Copy the code
Ok, let’s draw the ground rules for rewriting from the example above:
-
When a subclass overrides a parent method, the subclass can change the mandatory parameters of the parent to optional parameters, and the subclass can add optional parameters.
-
When a subclass overrides a parent method, the subclass cannot add mandatory parameters, cannot remove parameters, cannot change optional parameters to mandatory parameters, cannot change the parameter type to another type, but can delete the parameter type of the parent class.
-
When a subclass overrides a parent method, the return value type of the subclass method must be the same as that of the parent. If the parent class does not specify a return type, then the child method can specify its own return type.
-
When a subclass overrides a superclass method, the subclass method must be public only if the superclass method is public. If the superclass method is protected, the subclass can be public or protected. My examples above are not complete, so you can test them locally.
-
After a subclass overrides a parent method, it can still access the overridden method via parent::.
-
If a parent class is “strong” and does not want its method overridden by a subclass, you can decorate the method with the final keyword.
-
When a subclass overrides a method of its parent class, the static state of the subclass method must be the same as that of the parent class. If the parent class is static, then the subclass method must also be static. If the parent class is non-static, then the subclass method must also be non-static. We’ll talk more about static in the next video.
In summary, when subclasses override methods, visibility restrictions for arguments must be the same or lighter than for the parent class’s weaker. With the exception of return values, a subclass’s return value can be more restrictive than a parent’s.
When the superclass method visibility is private, the following is normal:
Class Baro {private function testPrivate() {}} class Fool extends Baro {// $a protected function testPrivate($a){}} class fool1 extends Baro {\\ TestPrivate ($b){}} class fool-rule extends Baro {$b} public function testPrivate($d):array{}}Copy the code
In this example, the parent class defines private methods, which cannot be inherited from the parent class, so the child class can rewrite (add) any way it wants.
Attribute to rewrite
When a child class overrides a parent class property, it follows a principle: the visibility of the child class property must be the same or more relaxed as that of the parent class.
Let’s look at an example:
class MyClass1 { public $public = 'Public'; protected $protected = 'Protected'; private $private = 'Private'; function printHello() { echo $this->public; echo $this->protected; echo $this->private; } } $obj = new MyClass1(); echo $obj->public; Echo $obj->protected; Echo $obj->private; echo $obj->private; $obj->printHello(); $obj->printHello(); / / PublicProtectedPrivate, all can access the class in this kind of internal MyClass2 extends MyClass1 {$private private = 'Private2'; Public $public = 'Public2'; Protected $protected = 'Protected2'; Function printHello() {echo $this->public; echo $this->protected; echo $this->private; } } $obj2 = new MyClass2(); $obj2->printHello(); //Public2 Protected2, Private2; Class MyClass3 extends MyClass1 {public $private = 'Private3'; Public $protected = 'Protected3'; Public class MyClass4 extends MyClass1 {static public $public = 'Public4'; // Error: cannot override static;Copy the code
Ok, here’s a summary of the rules for property rewriting:
-
The static property of a child class must be the same as that of its parent class;
-
If the superclass property is public, then the subclass property must be public.
-
If the parent property is protected, the subclass can be public or protected;
-
If the superclass property is private, then the subclass can be any property, because the superclass private property is not visible to the subclass, so it does not affect the subclass to create a property of the same name.
Parent class Private property
But if we print out $obj2 from the example above, we see a hole in it:
print_r($obj2); //output: [private:MyClass2:private] => public [private:MyClass2:private] => public [private:MyClass2:private] => Protected2 [private:MyClass1:private] => private) // Both the private properties of the parent class existCopy the code
As you can see, as a subclass object, it contains two properties of the same name attr, one is its own private property and the other is actually inherited from the parent private property. All other properties of the parent class are overridden and invisible. In other words, only the private variables of the parent class are not overridden by the subclass and are inherited by the subclass.
So what happens when a subclass object is called by a superclass method? Let’s look at this example:
Class POP {// define parent class private properties and public method print properties; private $attr = "father"; public function getFields() { echo get_class($this); var_dump($this); return $this->attr; }} class COC extends POP {// extends POP; protected $attr = "Child"; } $obj = new COC(); var_dump($obj->getFields()); // Call the parent method, output:fatherCopy the code
In this example, as long as the parent property is private, regardless of the visibility of the subclass property, the output is “father”, which is contrary to common sense. Is $this invalid?
Of course not. We know from the $obj print that when the parent property is private, the subclass will inherit and retain the value of the parent class, and will not be overridden. There are two attr properties with the same name in the object.
Two properties of the same name, how do I print them? The solution for PHP is: $this->attr ($this->attr); $this->attr ($this->attr); $this->attr ($this->attr); This mechanism can be understood as the “proximity principle “:
When an object contains two properties of the same name, the properties of that class are returned from which the class is called.
Finally, we mentioned that a subclass method can use parent:: to access the overridden parent method, but a subclass cannot use parent:: to access the overridden parent property unless the parent property is static. Ok, we’ve talked a lot about statics today, so in the next section we’ll talk about static properties and methods in PHP classes.
Class is over ~!
Summary is not easy, please do not reprint, otherwise don’t blame the old man you are welcome!
References:
www.php.net PHP official documentation
Zero basic PHP study notes tomorrow technology
How to access subclass properties in PHP superclass methods www.zhai14.com/blog/how-to… Debugging code farmers