Home  >  Article  >  Backend Development  >  求助:关于继承错误的问题

求助:关于继承错误的问题

WBOY
WBOYOriginal
2016-06-23 14:02:54768browse

php版本5.4.12
PHP Strict Standards:  Declaration of db_attach::needBy() should be compatible with spModel::needBy($id, $value) in C:\php\web\db\db_attach.php on line 25

重写的时候参数不一致,看了网上的方法
1.给重写的函数参数初始化
2.设置error_reporting为error_reporting(E_ALL & ~(E_STRICT | E_NOTICE))(我是在php.ini添加的)

总是无法屏蔽这个错误。请问有什么办法吗?谢谢


回复讨论(解决方案)

再加一个 & ~E_DEPRECATED

通常此类错误时不该屏蔽的
按着提示修改一下,可以使程序更健壮

还是不行啊,重写不是不要求参数一致吗?这个要改,程序动的太多了,难道要换版本?

贴出你的代码,只贴相关的部分
这样空对空的说不清楚

class access{
public function needBy($field, $value)
{
return $this->find(array($field=>$value));
}
}

class dbconect extends access  
{  



function needBy($id)
{
return $this->find(array('id'=>$id),'','uid,path');
}

}

谢谢,帮忙看看,还需要什么随时提供

等不急了!
测试了一下

class c { function test( $a ) { return 1; } }  class cc extends c { function test() { return null; } }   $cc = new cc(); 
Strict Standards: Declaration of cc::test() should be compatible with c::test($a) in ...

这很简单,重写方法的函数签名本应该就和基类函数是一致的,这也是符合自然规律的,因为override本来就是覆盖的意思嘛,既然覆盖了,那么就应该和原函数一致,不然怎么能“盖”的住呢

呵呵,贴的比我写还快

access::needBy($field, $value) 有两个参数
dbconect 继承于 access  
dbconect::needBy($id) 只有一个参数,所以出错。原因上面已说
只需
dbconect::needBy($id, $null=null) 加一个可缺省的参数
就可以了

这段写的,我觉得是版本问题

Reproduce code:
---------------
// 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();
?>

// 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();
?>

Expected result:
----------------
None of the code blocks should trigger an error (my personal preference) or both code blocks must trigger a notice.

Actual result:
--------------
First block triggers:
Strict standards: Declaration of cc::test() should be compatible with that of c::test() in strict_test.php on line 4

Second does nothing at all.

缺省的参数的方法不错,测试通过,谢谢版主,学习了,以后还要经常请教,非常感谢!

当然与版本有关啦
php 用的人太多了,所以开发者们有意的制造些障碍
到 php5.5 就可能象 java 一样严厉了
于是就成了解释执行的 java ?? 永远跟在 java 后面吃屎

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn