PHP:What 如果在同一个 class 中使用 __construct 和 class 名称定义构造函数?

PHP:What if the contructor is defined with __construct and classname in same class?

是的,这是相当主观的,但这将有助于理解渲染行为并确定 class 实体形成中实践的优先级。

下面是我的文字描述:

class anyclass{

function __construct(){
  //Any Required initialization

}



function anyclass(){
  //Any Required initialization

}


} 

以下是与此片段相关的问题:

  1. 是否可以执行,如果可以,如何执行?
  2. 如果是,并且两者都被认为是构造函数,这将给出 最终输出?
  1. 是的,它是可执行的。
  2. __construct总是赢在PHP 5.

    class A
    {
        function __construct()
        {
            echo 1;
        }
    
        function A()
        {
            echo 2;
        }
    }
    
    new A(); // prints 1 with PHP 5, prints 2 with PHP 4