Home >Backend Development >PHP Problem >What is the method of calling the parent class constructor in php?

What is the method of calling the parent class constructor in php?

coldplay.xixi
coldplay.xixiOriginal
2020-08-06 13:52:323821browse

php calls the parent class constructor: first, the parent class constructs the function first, the code is [public function __construct()]; then use [parent::__construct()] to call the parent class constructor.

What is the method of calling the parent class constructor in php?

php calls the parent class constructor method:

1. Use the function [parent::__construct()] to call The parent class constructor

code is as follows;

<?php
class MyClass    //父类
{
    public function __construct()    //父类构造函数
{
    echo "父类的构造函数";
}
}
class ChildClass extends MyClass     //子类 
{
    public function __construct()    //子类构造函数
{
        echo "子类的构造函数"."<br>";
        parent::__construct();    //调用父类构造函数
    }
}
$childClass = new ChildClass();    //对类的实例化
?>

2. Running results

Constructor of the subclass

Constructor of the parent class

Related learning recommendations:php programming(video)

The above is the detailed content of What is the method of calling the parent class constructor in php?. 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