Method declaration must be compatible with parent methods in PHP

  Strict Standards: Declaration of childClass :: customMethod () should be compatible with that of parentClass :: customMethod ()

What are the possible causes of this error in PHP? Where can I find information on what it means to be compatible?

+75
methods php
Jun 25 2018-10-06T00:
source share
5 answers

childClass::customMethod() has different arguments or a different access level (open / closed / protected) than parentClass::customMethod() .

+96
Jun 25 '10 at 3:40
source share

This message means that there are certain possible method calls that may not work at run time. Suppose you have

 class A { public function foo($a = 1) {;}} class B extends A { public function foo($a) {;}} function bar(A $a) {$a->foo();} 

The compiler only checks the call to $ a-> foo () for the requirements of A :: foo (), which does not require any parameters. However, $ a may be an object of class B that requires a parameter, and therefore the call will fail at runtime.

This, however, can never fail and does not cause an error.

 class A { public function foo($a) {;}} class B extends A { public function foo($a = 1) {;}} function bar(A $a) {$a->foo();} 

Thus, no method can have more required parameters than its parent method.

The same message is also generated when type hints do not match, but in this case PHP is even more restrictive. This gives an error:

 class A { public function foo(StdClass $a) {;}} class B extends A { public function foo($a) {;}} 

how it does:

 class A { public function foo($a) {;}} class B extends A { public function foo(StdClass $a) {;}} 

This seems more restrictive than it should be, and I guess this is due to internal functions.

Differences in visibility cause a different error, but for the same main reason. No method can be less noticeable than its parent method.

+27
Feb 11 2018-12-12T00:
source share

If you want to save the OOP form without turning off any error, you can also:

 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(); // ... } } 
+16
Dec 06 '14 at 7:47
source share

Just to extend this error in the context of the interface if you enter parameter types, for example:

interface A

 use Bar; interface A { public function foo(Bar $b); } 

Class B

 class B implements A { public function foo(Bar $b); } 

If you forget to include the use statement in your implementation class (class B), you will also get this error, even if the method parameters are identical.

0
Mar 21 '17 at 9:41
source share

Abstract class A

 abstract class A { function foo(); } 

Child class B

 class B { function foo($a=null, $b=null, $c=null) { //do what you want with the parameters. } } 

Instance:

 $b = new B(); $b->foo(); $b->($m, $n, $l); 

The goal is to make both $b->foo and $b->($m, $n, $l) workable.

-one
Jul 20 '17 at 3:56 on
source share



All Articles