Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

oop

php

I know there are a couple of similar questions here in StackOverflow like this question.

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

class Foo {     public function bar(Array $bar){} }  class Baz extends Foo {     public function bar($bar) {} } 

Strict standards: Declaration of Baz::bar() should be compatible with that of Foo::bar()

In other OOP programming languages you can. Why is it bad in PHP?

like image 947
Kaern Stone Avatar asked Nov 16 '12 19:11

Kaern Stone


People also ask

Is method overriding possible in PHP?

Function overloading and overriding is the OOPs feature in PHP. In function overloading, more than one function can have same method signature but different number of arguments. But in case of function overriding, more than one functions will have same method signature and number of arguments.

Can override have different parameters?

No, while overriding a method of the super class we need to make sure that both methods have same name, same parameters and, same return type else they both will be treated as different methods.

What happens if we change the arguments of overriding method?

4) What happens if we change the arguments of overriding method? If we change the arguments of overriding method, then that method will be treated as overloaded not overridden.

How can we stop overriding in PHP?

Avoiding Overriding of method php class A { final function display() { echo "Inside class A"; } } class B extends A { function display() { echo "Inside class B"; } } ?> So, using the keyword final infront of the method name prevents it from being overriden.


2 Answers

In OOP, SOLID stands for Single responsibility, Open-closed, Liskov substitution, Interface segregation and Dependency inversion.

Liskov substitution principle states that, in a computer program, if Bar is a subtype of Foo, then objects of type Foo may be replaced with objects of type Bar without altering any of the desirable properties of that program (correctness, task performed, etc.).

In strong-typed programming languages, when overriding a Foo method, if you change the signature in Bar, you are actually overloading since the original method and the new method are available with different signatures. Since PHP is weak typed, this is not possible to achieve, because the compiler can't know which of the methods you are actually calling. (hence the reason you can't have 2 methods with the same name, even if their signatures are different).

So, to avoid the violation of Liskov Substituition principle, a strict standard warning is issued, telling the programmer something might break due to the change of the method signature in the child class.

like image 178
Tivie Avatar answered Sep 22 '22 08:09

Tivie


I know I am late to the party but the answers don't really spell out the actual problem.

The problem is PHP doesn't support function/method overloading. It would be difficult to support function overloading in an untyped language.

Hinting helps. but in PHP it is very limited. Not sure why. For example you cannot hint a variable is an int or Boolean yet array is fine. Go figure!

Other object orientated languages implement this using function overloading. Which is to say the signature of the function is obviously different.

So for example if the following was possible we would not have an issue

class Foo {     public function bar(Array $bar){         echo "Foo::bar";     } }  class Baz extends Foo {     public function bar(int $bar) {         echo "Baz::bar";     } }   $foo = new Baz(); $bar = new Baz(); $ar = array(); $i = 100;  $foo->bar($ar); $bar->bar((int)$i);  would output  Foo::bar Baz::bar 

Of course when it came to constructors the php developers realised they have to implement it, Like it or not! So they simply suppress the error or not raise it in the first case.

Which is silly.

An acquaintance once said PHP implemented objects only as a way of implementing namespaces. Now I am not quite that critical but some of the decisions taken do tend to support that theory.

I always have maximum warnings turned on when developing code, I never let a warning go by without understanding what it means and what the implications are. Personally I don't care for this warning. I know what I want to do and PHP doesn't do it right. I came here looking for a way to selectively suppress it. I haven't found a way yet.

So I will trap this warning and suppress it myself. Shame I need to do this. but I am strict about STRICT.

like image 38
DeveloperChris Avatar answered Sep 25 '22 08:09

DeveloperChris