Home  >  Article  >  Backend Development  >  Inheritance of PHP constructors

Inheritance of PHP constructors

不言
不言Original
2018-04-26 11:00:041441browse

This article introduces the inheritance of PHP constructors, which has certain reference value. Now I share it with you. Friends in need can refer to it

// ===Notes Part 1= ==

/*
// Inheritance of constructor methods

Constructor methods can be inherited
The principles of inheritance are the same as ordinary methods.

And then , if the subclass also declares a constructor, the constructor of the parent class will be overwritten.
If the constructor of the parent class is overwritten, naturally, only the new constructor in the subclass will be executed.
*/



// ===Code part 1===

class Human {
    public function __construct() {
        echo &#39;呱呱坠地!<br >&#39;;
    }
}class Stu extends Human {}$ming = new Stu(); // 呱呱坠地!// 这说明构造函数也是可以继承的



// ===Code part 2===

class Emperor extends Human {
    public function __construct() {
        echo &#39;红光满屋,终日不散<br >&#39;;
    }
}$zhu = new Emperor();echo &#39;<hr >&#39;;



// ===Notes Part 3===

/*
If there is a constructor when inheriting from a subclass,
should be added to the constructor Previous sentence:
parent::__construct();

Then write your own business logic.
*/

Related recommendations:

Detailed explanation of PHP constructor

The above is the detailed content of Inheritance of PHP constructors. For more information, please follow other related articles on the PHP Chinese website!

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