Home > Backend Development > PHP Tutorial > Detailed explanation of inheritance method examples of PHP constructor

Detailed explanation of inheritance method examples of PHP constructor

怪我咯
Release: 2023-03-12 19:48:01
Original
1282 people have browsed it

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();
?>
Copy after login

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();
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template