Home > Article > Backend Development > What is the method of calling the parent class constructor in php?
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.
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!