今天把原来一份很老的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. firebug js版

    1.有些时候如 ie6 7 8 你觉得F12 不好用的话  你可以直接 把这两个js 引用到html 里面 <script src="https://getfirebug.com/fi ...

  2. 对C#泛型实例化对像--转

    最近在编写一套开发框架结构主要应用.Net 3.5以上的框架开发与应用.在此框架中应用了较多的泛型.下面来讲讲对泛型的实例化,以代码为例,如: public class A { } public cl ...

  3. [转载]__type_traits

    在STL中为了提供通用的操作而又不损失效率,我们用到了一种特殊的技巧,叫traits编程技巧.具体的来说,traits就是 通过定义一些结构体或类,并利用模板类特化和偏特化的能力,给类型赋予一些特性, ...

  4. Python网页爬虫(一)

    很多时候我们想要获得网站的数据,但是网站并没有提供相应的API调用,这时候应该怎么办呢?还有的时候我们需要模拟人的一些行为,例如点击网页上的按钮等,又有什么好的解决方法吗?这些正是python和网页爬 ...

  5. #include<unistd.h>存在linux中,含有系统服务的函数

    #include<unistd.h> linux标准库#include <unistd.h>与windows的#include <windows.h>(C语言开发) ...

  6. C语言零移位操作

    给定一个整形数组要求把其中的零元素移动到数组的末尾 非零元顺序保持不变 以下采用两种方法实现 #include <stdlib.h> #include <stdio.h> #i ...

  7. JSON带来编程界怎样的描述

    JSON是一套数据对象组织格式,从程序员的角度观看,他是以种非常易读易写的形式来描述一种key-value的数据组织.全名称JavaScript Object Notation,从名称上可看已经说明他 ...

  8. PAT - IO-01. 表格输出(5)

    题目: 本题要求编写程序,按照规定格式输出表格. 输入格式: 本题目没有输入. 输出格式: 要求严格按照给出的格式输出下列表格: ----------------------------------- ...

  9. Flowplayer-一款免费的WEB视频播放器 转 - helloweba.com

    Flowplayer支持播放flv.swf等流媒体以及图片文件,能够非常流畅的播放视频文件,支持自定义配置和扩展. 1.加载flowplayer.js 在要播放视频的页面的head之间加入flowpl ...

  10. UIAlertView的使用方法

    UIAlertView的使用方法 1. 最简单的用法 UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示" mes ...