Deprecated: Methods with the same name as their class

最近刚把php版本从5.6切换到7.0上,但是再重新打开项目时,报如下错误: Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Smarty has a deprecated constructor in /www/platform/library/Platform/View/Smarty/Smarty.class.php 原来是smarty模板类还是使用了php4的构造函数的写法,所以找到smarty类后,找到与类同名的函数,将函数名改 为`__construct`即可。 PHP OOP使用和类名相同的方法名作为构造方法,是 PHP4 的写法,PHP 5中同时支持`__construct`和类同名方法,但`__construct`方法具有优先性。 PHP 7开始使用和类名相同的方法名作为构造方法会报E_DEPRECATED级别的错误,提示在未来版本中会彻底抛弃类同名方法作为 构造函数。 但程序仍然会正常执行。 class a{ function a(){ } } Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP 的解决方法是使用`__construct`作为 构造方法的方法名。即: class a{ function __construct(){ } }
联系我们

邮箱 626512443@qq.com
电话 18611320371(微信)
QQ群 235681453

Copyright © 2015-2022

备案号:京ICP备15003423号-3