I came across a question in OOP in PHP. I tried to implement an abstract parent class method and from the child class, I have to use it with a variable number of arguments.
Here is the error thrown :
PHP Fatal error: Declaration of Square::getArea($length) must be compatible with Shape::getArea()
And the classes :
abstract class Shape {
abstract protected function getArea();
}
class Square extends Shape {
public function getArea($length)
{
return pow($length, 2);
}
}
class Triangle extends Shape {
public function getArea($base, $height)
{
return .5 * $base * $height;
}
}
I could use the child's __construct()
methods to set the properties of the different shapes at the initiation time but I'd like to know if another way exists and allows me to define variable list of parameters.
Thanks in advance.