I get this error Strict standards: Declaration of Boo::sayHello() should be compatible with Foo::sayHello($param = false) in C:\...
Because I don't set $param = false or true in the extended child class.
class Foo
{
var $message = 'Hello Foo';
public function sayHello($param = false)
{
return $this->message;
}
}
class Boo extends Foo
{
public function sayHello($param)
{
return 'Hello Boo';
}
public function sayWorld($b)
{
return 'Hello World';
}
}
$boo = new Boo;
var_dump($boo->sayHello('a'));
var_dump($boo->sayWorld('b'));
Is it possible to overwrite completely or redefine the method in the parent class?
By the way, is it a bad practice by doing so?
The reason for the error is that the number of parameters on
Boo::sayHello()does not match the parentFoo::sayHello(), by having a default of$param=falseonFoo::sayHello()this function can be called with no parameters where as the child class functionBoo::sayHello()requires a parameter.Consider the following function:
The compiler only checks the correctness of this function call against
Foo::sayHello()as any objects of sub-typeFooshould have the same method signature forsayHello().If you passed an object of
Booto the above function this would not work as expected due to the difference in method signatures/required parameters.You should be able to pass child class objects in place of parent objects - by redefining parent functions with different signatures you are going against this and may result in runtime errors.