The class name and method name in php can be the same. If the method name is the same as the class name and there is no __construct, then the method will be treated as a constructor. If it is used as a constructor and there is no [parent::__construct();], then the constructor of the parent class will not be executed.
If the method name has the same class name and there is no __construct, then the method will be treated as a constructor.
(Recommended tutorial: php video tutorial)
If it is used as a constructor and there is no parent::__construct();, then the constructor of the parent class will still not implement.
Example:
//php 5.6 class father{ public function __construct() { echo __METHOD__; } } class son extends father{ //public function __construct() { // parent::__construct(); // echo __METHOD__; //} public function son() { //parent::__construct(); echo __METHOD__; } } $a=new son();
Related recommendations:php training
The above is the detailed content of Can the class name and method name in php be the same?. For more information, please follow other related articles on the PHP Chinese website!