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 後面吃屎