Amit Dhamu

Software Engineer

Extending Class Methods in PHP 7

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)
    {
         
    }   
}

 php7

Deprecations in PHP 7Migrating to PHPUnit 8

Show Comments

Made with by Amit Dhamu.
© MMXX. All rights reserved.