This article mainly introduces the inheritance method of phpconstructor. It analyzes examples and summarizes various common situations of constructor inheritance. Friends who need it can refer to it
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.
The above is the detailed content of Detailed explanation of inheritance method examples of PHP constructor. For more information, please follow other related articles on the PHP Chinese website!