The construction method __construct()
is a special method unique to the class
structure. This method is specified by the system. The developer can When defining, you only need to write it once. After the class with constructor
instantiates the object
, the object
will be automatically called. This article will take you through it. Take a look.
1. The difference between the construction method and the ordinary method The difference is that Construction method will be automatically called immediately when the object is obtained through class instantiation, while ordinary methods need to be called manually.
2. If the construction method has parameters
<?php class People{ public $name; private $sex; protected $height; public function __construct(){ echo "Knowledge is power!"; } public function Hello(){ echo "你好,世界!"; } } //new People; 两者差别在于是否有参数 $man =new People();//构造方法自动调用 echo "<br>"; $man->Hello();//普通方法手动调用
输出:Knowledge is power! 你好,世界!
Recommended: 《2021 PHP Interview Questions Summary (Collection)》《
php video tutorial》
The above is the detailed content of Detailed explanation of the __construct() construction method in PHP. For more information, please follow other related articles on the PHP Chinese website!