PHP Strict standards:Declaration of … should be compatible with that of…(转)
今天把原来一份很老的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 messageerror_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 messageerror_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…(转)的更多相关文章
- php 错误 Strict Standards: PHP Strict Standards: Declaration of .... should be compatible with that of 解决办法
错误原因:这是由于 php 5.3版本后.要求继承类必须在父类之后定义.否则就会出现Strict Standards: PHP Strict Standards: Declaration of ... ...
- Strict Standards: Declaration of UserModel::toJSON() should be compatible with that of BaseModel::toJSON()
使用php报了这个错误: 错误的意思是: 严格标准: usermodel中的 toJSON() 方法 应该 同 BaseModel中的toJson() 方法是兼容的. php要求 子类的方法如果同父 ...
- PHP中Strict Standards错误解决方法二
在PHP5.3.3 中安装wordpress 3.0.1 ,在安装时出现错误:Strict Standards: PHP Strict Standards: Declaration of Walker ...
- php方法重写:Declaration of should be compatible with that
<?php// this code does trigger a strict messageerror_reporting( E_ALL | E_STRICT ); class cc exte ...
- PHP Strict Standards:问题解决
异常信息: ( ! ) Strict standards: Declaration of SugarEmailAddress::save() should be compatible with tha ...
- 安装ECMall后报PHP Strict Standards错误,请问如何解决
Strict Standards: Non-static method ECMall::startup() should not be called statically in /htdocs/ecm ...
- 已解决:Strict Standards: Only variables should be passed by reference in
今天安装ecshop的时候最上面出现了一个错误提示:Strict Standards: Only variables should be passed by reference in F:\www.x ...
- 已解决: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 ...
- 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 ...
随机推荐
- Solr配置集群
1.主机SolrConfig.xml <requestHandler name="/replication" class="solr.ReplicationHand ...
- UIWebView取消长按放大(用于长按识别二维码)
禁用长按UIWebView时放大镜及选择功能: //通过js调用 - (void)webViewDidFinishLoad:(UIWebView*)webView { // Disable user ...
- java 安卓开发之文件的读与写
java文件的读与写,代码: String file="user.txt"; private void writeFileData(String str1, String str2 ...
- 【USACO 3.2.6】香甜的黄油
[描述] 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油.当然,他将付出额外的费 ...
- 兄弟连王牌PHP课程送三重豪礼啦!
兄弟连PHP就业办课程送三重豪礼啦! 惊喜一:报名9月23日班级,保障薪资直涨1000元! 9月报名学习,春节后就业,正是企业招聘的黄金高峰期,一年中拿到高薪最好的时节! 惊喜二:兄弟连云课堂900元 ...
- Destoon QQ互联一键登录审核不通过的解决方案
在QQ互联上申请帐号之后提交了审核, 后台填写APPID和KEY之后自己申请的QQ号可以正常登录,但QQ互联审核的时候一直审核不通过说是“您的网站审核未通过,原因是“点击QQ登录按钮提示登录失败或出现 ...
- PHP转换IP地址到真实地址的方法详解
本篇文章是对PHP转换IP地址到真实地址的方法进行了详细的分析介绍,需要的朋友参考下 想要把IPv4地址转为真实的地址,肯定要参考IP数据库,商业的IP数据库存储在关系型数据库中,查询和使用都非常 ...
- WEB编码事项
标准 WEB开发标准是一系列标准的集合, 包含HTML结构标准.CSS表现标准.JS行为标准.代码标准.标准测试. 目标 WEB开发流程统一标准化,实现页面结构.表现.行为适当分离,提高页面易维护性, ...
- poj 3371 Flesch Reading Ease
http://poj.org/problem?id=3371 #include<cstdio> #include<cstring> #include<algorithm& ...
- 利用readwritelock简单模拟实现多线程下cache的系统
package cn.lyy.hibernate.many2one; import java.util.HashMap; import java.util.Map; import java.util. ...