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

coldplay.xixi
Release: 2023-03-03 22:16:01
Original
3802 people have browsed it

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();    //对类的实例化
?>
Copy after login

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!

Related labels:
php
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