First, the constructor
Classes with constructors Call this method each time a new object is created, so it is ideal to do some initialization before working with the object. If a constructor is defined in a subclass, the constructor of its parent class is not implicitly invoked. To execute the constructor of the parent class, you need to call parent::__construct ()in the constructor of the subclass. If a subclass does not define a constructor, it inherits from the parent class as a normal class method (if it is not defined as private). Second, access privileges private
The parent class private property does not inherit from the quilt class. Same. The parent class private method also does not inherit from the quilt class
Iii. understanding of Polymorphism
When an object invokes a method, the program looks for a method with the same name in the code area in memory, and the program can tell which method to call when there are multiple methods with the same name.
<?php
class Animal {
private $age =;
protected $name;
protected $sex = ' gong ';
The Public Function __construct ($name) { //subclass does not automatically call the parent class constructor when the constructor is defined, and the parent class constructor is not automatically invoked when a subclass has a defined constructor.
$this->name = $name;
}
Public Function call () { //polymorphic: When an object invokes a method, the program looks in the code area in memory for a method with the same name, and the program can tell which method is invoked when there are multiple methods with the same name.
}
}
Class Dog extends Animal {public
function call () {
echo ' Puppy name: '. $this->name. ' Sex: '. $this->sex;
Echo '
';
}
}
Class Cat extends Animal {public
function call () {
echo ' kitten name: '. $this->name. ' Sex: '. $this->sex;
Echo ' Age: '. $this-> $age; Output age error, notice:undefined variable:age
} //private property does not inherit from the Quilt class
$d = new Dog (' Jack ');
$c = new Cat (' Tom ');
$array = Array ($d, $c);
foreach ($array as $e)
$e->call ();
? >
The output results are:
Puppy name: Jack sex: Male
Kitten name: Tom sex: Male
notice:undefined variable:age in f:\xampp\webroots\object\ Extendstest.php on line 29
Fatal error:cannot Access Empty property in f:\xampp\webroots\ Object\extendstest.php on line 29