Why are static method signatures incompatible if I am not involving an interface or abstract class?

Viewed 31

Base class and inherited setup

class A
{
  static public function meow(string $x,string $y="default"){}
}

class B extends A
{
  static public function meow(string $y="default"){}
}

Output

PHP Fatal error:  Declaration of B::meow(string $y = 'default') must be compatible with A::meow(string $x, string $y = 'default') in ...

I have seen other similar issues that involve abstract classes or interfaces, which is not the case here.

Why am I forced to define the exact same parameters?

1 Answers

In Liskov Substitution Principle(LSP), The behavior of the sub-classes must respect the contract established in the super-class.

There cannot be a method in the subclass that goes against a behavior of the base class. This is called Historical Constraint.

Have a look on full article about LSP

Related