website/errors/parameter.notOptional.md
<?php declare(strict_types = 1);
class Foo
{
public function doFoo(int $i, int $j = 0): void
{
}
}
class Bar extends Foo
{
public function doFoo(int $i, int $j): void
{
}
}
The overriding method makes a parameter required that is optional in the parent method. In the example above, parameter $j has a default value in Foo::doFoo() (making it optional), but the overriding method Bar::doFoo() declares $j without a default value (making it required).
This violates the Liskov Substitution Principle. Code that calls $foo->doFoo(1) with only one argument would break when $foo is an instance of Bar, because Bar::doFoo() requires two arguments.
Make the parameter optional in the child class to match the parent:
class Bar extends Foo
{
- public function doFoo(int $i, int $j): void
+ public function doFoo(int $i, int $j = 0): void
{
}
}