PHP website Definition:
Copy Code code as follows:
A constructor is a special function in a class that is automatically invoked when an instance of a class is created with the new operator. This function becomes a constructor when the function has the same name as the class. If a class has no constructors, call the constructor of the base class, and if so, call its own constructor
such as a.php a Class A:
Copy Code code as follows:
<?php
Class a{
function __construct () {
echo ' Class A ';
}
}
B.php has Class B inherits Class A:
Copy Code code as follows:
<?php
Include ' a.php ';
Class B extends a{
function __construct () {
Echo ' 666666 ';
Parent::__construct ();
}
function index () {
echo ' index ';
}
}
$test =new B ();
In this case, B class has its own constructor, then instantiate Class B, automatically run the constructor, at this time the default does not run the constructor of the parent class, if you want to run the parent constructor, to declare parent::__construct ();
Copy Code code as follows:
<?php
Include ' a.php ';
Class B extends a{
function index () {
echo ' index ';
}
}
$test =new B ();
When Class B does not have its own constructor, the constructor of the parent class is executed by default.