This example describes the inheritance method of the PHP constructor. Share to everyone for your reference. Specifically as follows:
in the first case: When a subclass does not define a constructor, the default inheritance. Example:
<?php
class a{public
$name;
function __construct () {
echo $this->name= "cockroach";
}
}
Class B extends a{
}
$bb = new B ();
? >
Output result: Xiao Qiang
in the second case: subclasses define constructors, they are not inherited. Example:
<?php
class a{public
$name;
function __construct () {
echo $this->name= "cockroach";
}
}
Class B extends a{
function __construct () {
echo "bbbbbb subclass";
}
}
$BB = new B ();
? >
Output Result: BBBBBB Subclass
Third scenario: If you need to invoke the constructor of the parent class, you can use: Parent:: Parents class function or parent class Name:: Parent class function.
I hope this article will help you with your PHP program design.