d
Amit DhamuSoftware Engineer
 

Extending Class Methods in PHP 7

1 minute read 00000 views

Extended class methods' argument list now need to match their parent method signature in order to avoid a warning.

Problem

class Foo
{
    public function save($id)
    {

    }
}

class Bar extends Foo
{
    public function save()
    {

    }
}

(new Bar)->save();

The above code will yield a warning.

Warning: Declaration of Bar::save() should be compatible with Foo::save($id)

The solution is to to declare the extended method as below:

class Bar extends Foo
{
    public function save($id)
    {

    }
}