In
PHP, type inheritance uses the extends keyword and can inherit at most one parent class.
PHPMultiple inheritance is not supported. This article mainly introduces
PHPIn the class and object (inheritance), the required friends can refer to the following
Brief introduction
In PHP, type inheritance uses the extends keyword and can inherit at most one parent class, and PHP does not support multiple inheritance.
Class MyClass {public $dat = 0; Public function construct ($dat) { $this->dat = $dat; } Public Function Getdat () { return ' $this->dat\n '; }} class MySubClass extends MyClass {public functio n Getdat () { return "dat: $this->dat\n"; }} $a = new MyClass (3); $b = new MySubClass (4); Echo $a->getdat ( ); 3 echo $b->getdat (); Dat:4
Method overrides
Including constructors, subclasses can redefine a class method with the same name to override the parent class method. The following rules apply when overriding:
1. In addition to constructors, the function's argument list must be the same when overridden by other functions
2. Including constructors, when methods are overwritten, the parent class method is not automatically called when the child class method is called
3. If the parent class wants to suppress the method quilt class override, you can use final to declare the method, and if the subclass still overrides the parent class method, an error will occur
Class MyClass { Private $name = ""; Public $num = 0; Public $str = ""; Public function construct ($name) { $this->name = $name; $this->num = +; $this->str = "None"; } Public Function GetName () { return $this->name; }} class MySubClass extends MyClass {public function C Onstruct ($name, $str) { parent::construct ($name); Call the parent class method $this->num = "0"; $this->str = $str; Echo Parent::getname (). " \ n "; Call the parent class method } public function GetName () { return parent::getname (). " $this->str\n "; Call the parent class method }} $b = new MySubClass ("MyName", true); MyName echo $b->getname (); MyName1 class MyClass { final public function getName () { }}
Attribute redefinition
In a subclass, you can access public and protected property members in the parent class, unless you redefine the own property with the same name, and the properties in the parent class will not be accessible.
method is different, the subclass can still access the parent class method after the method is overwritten.
Class MyClass {public $a = 1; protected $b = 2; Private $c = 3; Public Function F1 () { echo "MyClass f1\n"; echo "\ $a: $this->a; \ $b: $this->b; \ $c: $this->c;\n "; } protected function F2 () { echo "MyClass f2\n"; echo "\ $a: $this->a; \ $b: $this->b; \ $c: $this->c;\n "; } Private Function F3 () { echo "MyClass f3\n"; }} class MySubClass extends MyClass {public $b = 22;
public $c =; Public Function F1 () { echo "MySubClass f1\n"; Inherit the $ A property in the parent class, using the echo "\ $a: $this->a; \ $b: $this->b; \ $c: $this->c;\n "; Invokes the same name Method Parent::f1 () in the parent class ; Inherit the F2 () method from the parent class, directly using the $this->f2 (); } The parent class's F3 () is private, and the definition here is unrelated to the parent class public function F3 () { echo "MySubClass f3\n"; }} $b = new MySubClass; $b->f1 (); echo "\ n"; /* MySubClass F1 $a: 1; $b: 22; $c: 33; MyClass F1 $a: 1; $b: 22; $c: 3; MyClass F2 $a: 1; $b: 22; $c: 3; */$b->F3 (); echo "\ n"; /* MySubClass F3 */
When you redefine a parent class (with the same name) property, the accessibility of the property can become more open, but not more restrictive, that is, the public property in the parent class cannot be modified in the subclass to the private property.
If the parent class method is called through a subclass object, the parent class method accesses the property, and the properties of public and protected are accessed to the subclass version for the redefined property of the same name, and the private property is accessed to the parent class version. It can also be understood that the public and protected properties can be redefined (the version of the parent class is redefined and thus does not exist), and private is not redefined (the attribute in the parent class still exists, accessed through the parent class method, and has no relation to the attribute of the same name in the subclass).
Class MyClass {public $a = 1; protected $b = 2; Private $c = 3; Public Function F1 () { echo "\ $a: $this->a; \ $b: $this->b; \ $c: $this->c;\n "; } } class MySubClass extends MyClass {public $a = one; Must be public protected $b = 22;//must be protected or public private $c =; Public Function F2 () { echo "\ $a: $this->a; \ $b: $this->b; \ $c: $this->c;\n "; } } $b = new MySubClass; $b->f1 (); $a: 11; $b: 22; $c: 3; $b->f2 (); $a: 11; $b: 22; $c: 33;
Scope resolution Operator::
A colon is often used to access class constants, class static variables, and also to invoke the parent version of a method when overridden. It also includes keywords such as parent, self, and static.
Class MyClass { const NAME0 = "MyClass"; class constant public static $id 0 = 0; Class Variable public function put () { //method of overriding quilt class echo "MyClass put () \ n"; }} class MySubClass extends Myclass
{ Const NAME1 = "MySubClass"; public static $id 1 = 1; Public function put () { parent::p ut (); Call the parent class version of the object method echo PARENT::NAME0. "\ n"; Parent class constant Echo Parent:: $id 0. "\ n"; The parent class variable echo self::name1. \ n "; Subclass Constant Echo Self:: $id 1. "\ n"; Sub-class variable echo static::name1. "\ n"; The Sub -category echo Static:: $id 1. "\ n"; Sub-class variable }} $a = "MyClass"; $ca = new MyClass; $CB = new MySubClass; $CB->put (); Echo MYCLASS::NAME0. "\ n"; echo MyClass:: $id 0. "\ n"; echo $a:: NAME0. "\ n"; echo $a:: $id 0. "\ n"; echo $CA:: NAME0. "\ n"; echo $CA:: $id 0. "\ n";
When accessing members of a parent class in a subclass, avoid using the parent class name directly, instead use Parent:: To avoid breaking the wrapper of the parent class.
Final
A method that is declared final cannot be overridden by a class, and if the class is declared final, the class cannot be inherited.
A class that is declared final cannot be inherited from the final class MyClass { private $dat; Public function construct ($dat) { $this->dat = $dat; } The final method cannot be overwritten, but this class is already the final class, and the method is not necessary to declare final public function Getdat () { return $this->dat; }}
Summarize
The above is a small part of the introduction of the PHP class and objects (inheritance), I hope to help you!!
Related recommendations:
PHP type constraint usage example _php tips
Extension and Inheritance Usage instance code for PHP classes
PHP Object Instantiation Single-case method