`
cloudtech
  • 浏览: 4609032 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

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版本后,要求继承类必须在父类之后定义,如果父类定义在前,继承类在后,就不会出现这个错误。尤其是bugs.php.net/bug.php?id=46851上面还煞有介事的给出了正反例:
<?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(); 
?> 
<?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。。。


分享到:
评论

相关推荐

    ecmall 兼容 php5.5以上版本

    在PHP5.5以上版本运行ecshop和ecmall出现的问题及解决方案

    php 出现Strict Standards: Only variables should be passed by reference in的解决方法

    这个问题多半是因为引用传递参数引起的...ECShop出现Strict Standards: Only variables should be passed by reference in的解决方法 今天安装ecshop的时候最上面出现了一个错误提示:Strict Standards: Only variables

    dubbo找不到dubbo.xsd报错

    - cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'. - schema_reference.4: Failed to read schema document '...

    phpdocument Strict Standards 错误解决办法

    phpdocument 错误: ...Strict Standards:Only variables should be passed by reference in D:\xampp\php\pear\PhpDocumentor\phpDocumentor\Smarty-2.6.0\libs\Smarty_Compiler.class.php on line 712

    http://www.w3.org/TR/html4/loose.dtd

    Authors should use the Strict DTD when possible, but may use the Transitional DTD when support for presentation attribute and elements is required. HTML 4 includes mechanisms for style sheets...

    解决dubbo找不到dubbo.xsd报错

    - cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'. - schema_reference.4: Failed to read schema document '...

    dubbo.xsd文件下载

    declaration can be found for element 'dubbo:application'. - schema_reference.4: Failed to read schema document 'http:// code.alibabatech.com/schema/dubbo/dubbo.xsd', because 1) could not find ...

    解决:dubbo找不到dubbo.xsd报错

    - cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'. - schema_reference.4: Failed to read schema document '...

    dubbo.xsd 约束文件 解决标签识别报红

    - cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'. - schema_reference.4: Failed to read schema document '...

    Openframeworks Graphics

    Openframeworks Graphics Introducing OpenGL for OF by Joshua Noble

    Underscore.php——Underscore.js的php移植版

    注意:Underscore.php的原作者是老牌PHP程序员,其中使用静态调用方式访问非静态方法时php会提示Strict Standards: Non-static method的严谨性警告,不过只需要在php脚本的头部添加: error_reporting(E_ALL & ~E_...

    dubbo.xsd文件资源

    - cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dubbo:application'. - schema_reference.4: Failed to read schema document '...

    marketo-rest-proxy:通过伪 REST 代理代理 Marketo 的 SOAP api

    市场休息代理 通过伪 REST 代理代理 Marketo 的 SOAP api。 要求 节点v0.10.26 安装 ... 'use strict' ; var request = require ( 'request' ) ; var CryptoJS = require ( 'crypto' ) ; var moment

    strict的反义词参考.doc

    strict的反义词参考.doc

    四六级作文常用句型总结

     3) It is necessary that steps should be taken to ...  4) In conclusion, it is imperative that ...  5) There is no easy method, but ...might be of some help.  6) To solve the above-mentioned ...

    php.ini-development

    &gt; should be used and that this feature ; should be disabled, as enabling it may result in issues when generating XML ; documents, however this remains supported for backward compatibility reasons. ; ...

    tsc-config-nodejs:在Node.js中配置打字稿

    Node.js的打字稿配置这是最适合我在Node.js中使用Typescript...-init我们进行以下更改 { "compilerOptions": { "target": "es6", "module": "commonjs", "outDir": "./dist", "rootDir": "./src", "strict": true, "mo

    wrk-node-benchmarking:wrk node.js http基准测试工具

    'use strict' ; var wrkNode = require ( 'wrk-node' ) ; var config = { // This required 'server' : { 'host' : process . env . TEST_HOST || 'http://localhost' , 'port' : process . env . TEST_PORT ||...

Global site tag (gtag.js) - Google Analytics