Can PHP child classes change the parameters of overridden methods?

I can override the PHP method in the child class and change the parameters in the signature as shown below.

class theParent { function myMethod($param1) { // code here } } class theChild extends theParent { function myMethod($param1, $param2) { // code here } } 

I tested this and it works great and does not cause any errors. My question is, is this bad form? Or the basic principle of OOP?

If the parent method is declared abstract, child signatures cannot be rejected. This is supposedly a mechanism to use if you need to enforce this aspect of the interface?

+6
source share
3 answers

You will send a string error message by standard without matching the parent class number for this method.

More details ...

Why is overriding method parameters a violation of strict standards in PHP?

+3
source

Till

 class theChild extends theParent { } 

This is a good example of OOP.

0
source

What you did is called redefinition, and thatโ€™s nothing wrong, but if you want the child classes to be bound to the parent's signature better, you use the interfaces as shown below. You should just give only signatures, and child classes to implement them as they are declared.

  interface theParent { function myMethod($param1) ; } class theChild extends theParent { function myMethod($param1) { // code here } } 

Hope this helps :)

0
source

Source: https://habr.com/ru/post/921433/


All Articles