wai*_*933 102 php methods standards-compliance
Strict Standards: Declaration of childClass::customMethod() should be compatible with that of parentClass::customMethod()
PHP中出现此错误的可能原因是什么?我在哪里可以找到有关兼容性的信息?
dav*_*nal 123
childClass::customMethod()有不同的参数,或不同的访问级别(公共/私人/受保护)parentClass::customMethod().
ldr*_*rut 36
此消息表示某些可能的方法调用可能在运行时失败.假设你有
class A { public function foo($a = 1) {;}}
class B extends A { public function foo($a) {;}}
function bar(A $a) {$a->foo();}
Run Code Online (Sandbox Code Playgroud)
编译器只根据A :: foo()的要求检查调用$ a-> foo(),这不需要参数.然而,$ a可能是B类的一个对象,它需要一个参数,因此调用在运行时会失败.
然而,这永远不会失败并且不会触发错误
class A { public function foo($a) {;}}
class B extends A { public function foo($a = 1) {;}}
function bar(A $a) {$a->foo();}
Run Code Online (Sandbox Code Playgroud)
因此,没有方法可能比其父方法具有更多必需参数.
当类型提示不匹配时也会生成相同的消息,但在这种情况下,PHP更具限制性.这给出了一个错误:
class A { public function foo(StdClass $a) {;}}
class B extends A { public function foo($a) {;}}
Run Code Online (Sandbox Code Playgroud)
就像这样:
class A { public function foo($a) {;}}
class B extends A { public function foo(StdClass $a) {;}}
Run Code Online (Sandbox Code Playgroud)
这似乎比它需要的更严格,我认为是由于内部.
可见性差异会导致不同的错误,但出于同样的基本原因.没有方法比其父方法更不可见.
Saj*_*azy 22
如果您想保留OOP表格而不关闭任何错误,您还可以:
class A
{
public function foo() {
;
}
}
class B extends A
{
/*instead of :
public function foo($a, $b, $c) {*/
public function foo() {
list($a, $b, $c) = func_get_args();
// ...
}
}
Run Code Online (Sandbox Code Playgroud)