Home  >  Article  >  Backend Development  >  PHP object-oriented inheritance constructor

PHP object-oriented inheritance constructor

零到壹度
零到壹度Original
2018-03-31 14:06:351915browse

This article mainly shares with you about PHP object-oriented inheritance constructor. Friends who need it can take a look.

Inheritance of constructor methods

Constructor methods can be inherited, and the principles of inheritance are the same as ordinary methods. Furthermore, if the subclass also When a constructor is declared, 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===

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

Then write your own business Logic.
*/

Related recommendations:

Principles of constructors during inheritance

Javascript object-oriented non-constructor inheritance

Constructor and destructor in PHP object-oriented function

The above is the detailed content of PHP object-oriented inheritance constructor. 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