問題詳細描述為:https://bugs.php.net/bug.php?id=46851
-
<?php
-
abstract class A {
-
// 方法無參數
-
public static function foo ( ) { echo 'bar' ; }
-
}
-
-
abstract class B extends A {
-
// 方法有參數
-
public static function foo ( $str ) { echo $str ; }
-
}
-
?>
如上面的代碼:類A中的foo方法無參數,類B在繼承A后重寫foo方法時加入了參數,因此會產生一個類似下面E_STRICT級別的警告:
Strict standards: Declaration of ... should be compatible with that of ...
解決方法:
-
<?php
-
abstract class A {
-
// 方法無參數
-
public static function foo ( ) { echo 'bar' ; }
-
}
-
-
abstract class B extends A {
-
// 方法有參數
-
public static function foo ( $str = NULL ) { echo $str ; }
-
}
-
?>
類B在重寫foo方法時為新加入的參數指定一個默認值即可。