今天把原来一份很老的PHP代码导入到了PaaS上,出现了许多Strict standards:Declaration of … should be compatible with that of…这样的错误,字面意思好像是说函数不匹配,看了下出错的函数,都是子类重写的基类函数。
上网搜索了一下,发现许多帖子基本都抄的一样,说什么这是由于 php5.3版本后,要求继承类必须在父类之后定义,如果父类定义在前,继承类在后,就不会出现这个错误。尤其是http://bugs.php.net/bug.php?id=46851上面还煞有介事的给出了正反例:

1
2
3
4
5
6
7
8
9
<?php
// this code does trigger a strict message
error_reporting( E_ALL | E_STRICT );
 
class cc extends c { function test() { return null; } }
class c { function test( $a ) { return 1; } }
 
$cc = new cc();
?>
1
2
3
4
5
6
7
8
9
<?php
// this code does NOT trigger a strict message
error_reporting( E_ALL | E_STRICT );
 
class c { function test( $a ) { return 1; } }
class cc extends c { function test() { return null; } }
 
$cc = new cc();
?>

并且讨论了出错的情况多半是由于用_autoload()对类进行自动的include,导致基类的定义在后面,子类定义在前面。

我看了下自己的代码,虽然确实也用到了autoload,但是都是显式的先导入了几个基类,并不存在这样的情况,而且将上面的正反例子试了一下,都会出现E_STRICT的警告。

真正原因:

其实如果子类重写方法的参数和基类不一样,只要给参数个默认值,使得编译器认为参数可以为空,保持重写方法与基类方法的函数签名相同就可以了。

经常用JAVA的同学肯定知道,在JAVA或者C++中,重写方法的函数签名本应该就和基类函数是一致的,我认为这也是符合自然规律的,因为override本来就是覆盖的意思嘛,既然覆盖了,那么就应该和原函数一致,不然怎么能“盖”的住呢~并且方法的重写多用在重写虚函数或者更明白的说就是重写接口的函数,如果重写的时候函数签名都不一致了,还要接口干嘛呢。。。

所以PHP的新版本中,我觉得定义这个E_STRICT的警告错误是很有用处的,要提醒程序员自己的重写方法到底对不对。

最后还是鄙视一下上面那些抄来抄去的帖子,如果某个语言连基类和子类定义的顺序都不能打乱,说明这个编译器非常有问题了,显然是bug。。。

PHP Strict standards:Declaration of … should be compatible with that of…(转)的更多相关文章

  1. php 错误 Strict Standards: PHP Strict Standards: Declaration of .... should be compatible with that of 解决办法

    错误原因:这是由于 php 5.3版本后.要求继承类必须在父类之后定义.否则就会出现Strict Standards: PHP Strict Standards: Declaration of ... ...

  2. Strict Standards: Declaration of UserModel::toJSON() should be compatible with that of BaseModel::toJSON()

    使用php报了这个错误: 错误的意思是:  严格标准: usermodel中的 toJSON() 方法 应该 同 BaseModel中的toJson() 方法是兼容的. php要求 子类的方法如果同父 ...

  3. PHP中Strict Standards错误解决方法二

    在PHP5.3.3 中安装wordpress 3.0.1 ,在安装时出现错误:Strict Standards: PHP Strict Standards: Declaration of Walker ...

  4. php方法重写:Declaration of should be compatible with that

    <?php// this code does trigger a strict messageerror_reporting( E_ALL | E_STRICT ); class cc exte ...

  5. PHP Strict Standards:问题解决

    异常信息: ( ! ) Strict standards: Declaration of SugarEmailAddress::save() should be compatible with tha ...

  6. 安装ECMall后报PHP Strict Standards错误,请问如何解决

    Strict Standards: Non-static method ECMall::startup() should not be called statically in /htdocs/ecm ...

  7. 已解决:Strict Standards: Only variables should be passed by reference in

    今天安装ecshop的时候最上面出现了一个错误提示:Strict Standards: Only variables should be passed by reference in F:\www.x ...

  8. 已解决:Strict Standards: Non-static method cls_image::gd_version() should not be called statically in...

    在安装Ecshop的时候,遇到两个⚠️问题: Strict Standards: Non-static method cls_image::gd_version() should not be cal ...

  9. Strict Standards: Only variables should be passed by reference

    <?php $tag="1 2 3 4 5 6 7"; $tag_sel = array_shift(explode(' ', $tag)); print_r($tag_se ...

随机推荐

  1. 安装php时,make步骤报错make: *** [ext/gd/gd.lo] Error 1

    安装PHP时,make步骤报错make: *** [ext/gd/gd.lo] Error 1 /usr/local/src/LAMP+memcahed+catci/php-5.4.0/ext/gd/ ...

  2. SWT/RAP计算器

    /** *2.测试 */ public class NofTest extends AbstractEntryPoint {        Text text,text2;    RemoteObje ...

  3. 解决java访问.netWebService的常见问题

    到公司没多久,写了一个java调用.net写的webService结果期间用各种方法测试都没有完成,总是抛出异常,最后直接使用SOAP消息去进行调用才成功了,具体代码如下,仅供参考:import ja ...

  4. 你好,C++(37)上车的人请买票!6.3.3 用虚函数实现多态

    6.3.3  用虚函数实现多态 在理解了面向对象的继承机制之后,我们知道了在大多数情况下派生类是基类的“一种”,就像“学生”是“人”类中的一种一样.既然“学生”是“人”的一种,那么在使用“人”这个概念 ...

  5. 个人Python常用Package及其安装

    为了避免每次重装系统时又要东翻西找,现在此记录一下目前常用的Python包安装过程. 1) Python: 2.7.11, 下载地址:www.python.org.由于个人喜欢使用PyQt4(其实是不 ...

  6. python中的几种遍历列表的方法比较

    python的内容非常丰富,给我们带来的便利很多,很多事情的表达方法有很大的多样性,比如我经常需要遍历一个列表,取它的下标和值,这个时候就有很多方法需要取舍一下才行. for循环遍历 l = [1,2 ...

  7. iOS开发——常用Runtime函数

    Runtime函数 1.可以通过NSObject的一些方法获取运行时信息或动态执行一些消息:1./*Returns a Boolean value that indicates whether the ...

  8. Python 函数传递list,传递dict 以及*args和**kargs

    函数之间传递list: def show(ll): for i in ll: print(i) show(['chen','hang','wang','yadan']) #============== ...

  9. Pku1947 Rebuilding Roads

    题意是给一棵树,问最少删掉几条边.使得剩下的子树中有节点个数为m个的 设f[i][j]表示i号点所在的子树中选了j个点至少需要删去f[i][j]条边. code: #include<cstdio ...

  10. uva10561 - Treblecross

    Treblecross is a two player game where the goal is to get three `X' in a row on a one-dimensional bo ...