Home >Backend Development >PHP Tutorial >Inheritance method of php constructor, php constructor inheritance_PHP tutorial
The example in this article describes the inheritance method of PHP constructor. Share it with everyone for your reference. The details are as follows:
The first case: When the subclass does not define a constructor, it is inherited by default. Example:
<?php class A{ public $name; function __construct(){ echo $this->name="小强"; } } class B extends A{ } $bb = new B(); ?>
Output result: Xiaoqiang
Second case: If the subclass defines a constructor, it will not be inherited. Example:
<?php class A{ public $name; function __construct(){ echo $this->name="小强"; } } class B extends A{ function __construct(){ echo "BBBBBB子类"; } } $bb = new B(); ?>
Output result: BBBBBB subclass
Third case: If you need to call the constructor of the parent class, you can use: parent::parent class function or parent class name::parent class function.
I hope this article will be helpful to everyone’s PHP programming design.