PHP报错:Deprecated Methods with the same name as their class will not be constructors in a future vers

务器的PHP版本从5.6升级到了7.2。

运行之前写的一个项目的时候,报错:

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP

这是啥错,没见过啊。

百度了一下,官方给的说法是:PHP7以后的类的构造函数必须是__construct。

所以找到对应的类,找到与类同名的函数,将函数名改为__construct即可。

PHP OOP使用和类名相同的方法名作为构造方法,是 PHP4 的写法,PHP 5中同时支持__construct和类同名方法,但__construct方法具有优先性。

PHP 7开始使用和类名相同的方法名作为构造方法会报E_DEPRECATED级别的错误,提示在未来版本中会彻底抛弃类同名方法作为构造函数。

但程序仍然会正常执行。

例如

<?php
        class a{
                function a(){
                        
                }
        }
?>

改成:

<?php
        class a{
                function __construct(){
                }
        }
?>

以上就是全部内容

有好的建议,请在下方输入你的评论。