Home >Backend Development >PHP Tutorial >Inheritance method of php constructor, php constructor inheritance_PHP tutorial

Inheritance method of php constructor, php constructor inheritance_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:07:25791browse

Inheritance method of php constructor, php constructor inheritance

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:

<&#63;php
class A{
 public $name;
 function __construct(){
 echo $this->name="小强";
 }
}
class B extends A{
 
}
$bb = new B();
&#63;>

Output result: Xiaoqiang

Second case: If the subclass defines a constructor, it will not be inherited. Example:

<&#63;php
class A{
 public $name;
 function __construct(){
 echo $this->name="小强";
 }
}
class B extends A{
 function __construct(){
 echo "BBBBBB子类";
 }
}
$bb = new B();
&#63;>

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/955400.htmlTechArticleInheritance method of php constructor, php constructor inheritance This article describes the inheritance method of php constructor. Share it with everyone for your reference. The details are as follows: The first situation: subclass...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn