Home >Backend Development >PHP Problem >Can the class name and method name in php be the same?

Can the class name and method name in php be the same?

王林
王林Original
2020-09-28 14:12:333357browse

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.

Can the class name and method name in php be the same?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn