[PHP] Deprecated: Methods with the same name as their class will not be constructor...

来源:互联网 发布:过山车大亨3 mac 中文 编辑:程序博客网 时间:2024/06/07 21:11

最近刚把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级别的错误,提示在未来版本中会彻底抛弃类同名方法作为

构造函数。
但程序仍然会正常执行。

  1. <?php
  2.         class a{
  3.                 function a(){
  4.                         
  5.                 }
  6.         }
  7. ?>
复制代码

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP 的解决方法是使用__construct作为

构造方法的方法名。即:

  1. <?php
  2.         class a{
  3.                 function __construct(){
  4.                 }
  5.         }
  6. ?>




阅读全文
0 0