Home >Backend Development >PHP Problem >What is the constructor method of PHP class?
What is the constructor method of PHP class?
The constructor of the PHP class refers to "__construct()". The constructor is a special method in the class. When the "new" operator is used to create an instance of the class, the constructor will It is called automatically, so it is usually used to perform some useful initialization tasks. This method has no return value.
Example
<?php class BaseClass { function __construct() { print "In BaseClass constructor\n"; } } class SubClass extends BaseClass { function __construct() { parent::__construct(); print "In SubClass constructor\n"; } } class OtherSubClass extends BaseClass { // inherits BaseClass's constructor } // In BaseClass constructor $obj = new BaseClass(); // In BaseClass constructor // In SubClass constructor $obj = new SubClass(); // In BaseClass constructor $obj = new OtherSubClass(); ?>
Recommended tutorial: "PHP"
The above is the detailed content of What is the constructor method of PHP class?. For more information, please follow other related articles on the PHP Chinese website!