1

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.

JazZ
  • 4,469
  • 2
  • 20
  • 40
  • 1
    `I could use the child's __construct() methods to set the properties of the different shapes at the initiation time` which really is the best approach – Mark Baker Jun 02 '17 at 18:23
  • But you could also use `func_get_args()` inside your `getArea()` method without specifying the arguments in the method definition – Mark Baker Jun 02 '17 at 18:24
  • I don't like much the `func_get_args()`. I find it kind of dirty. I'll keep the constructor way as you said it's the best approach. Thank you for the comments @MarkBaker – JazZ Jun 02 '17 at 18:26
  • Another alternative would be to use a single argument for all `getArea()` methods (including the one in the abstract) of `getArea(... $dimensions)` using the splat operator; followed by something like `list($base, $height) = $dimensions;` as the first line of the method – Mark Baker Jun 02 '17 at 18:30
  • 1
    Note also that if you've got empty methods in your abstract, you're not really making an abstract, you're making an interface. – Alex Howansky Jun 02 '17 at 18:35
  • Thank you both, I'll dig into the interface thing. – JazZ Jun 02 '17 at 18:40
  • @AlexHowansky could you please post an answer with details about the use of `interface` if it fits better to the situation ? – JazZ Jun 02 '17 at 18:41
  • See [this post](https://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class). – Alex Howansky Jun 02 '17 at 18:43
  • @AlexHowansky, thanks for the link. But the abstract class will implement more methods. But made me learn about interface ! \o/ – JazZ Jun 02 '17 at 19:18

2 Answers2

2

As in the comments under your question mentioned, there are several ways to solve your issue.

Class properties and the constructor That would be the easiest approach in my opinion. It 's easy and smart.

interface Shape
{
    protected function getShape();
}

class Square implements Shape
{
    protected $length;

    public function __construct(int $length)
    {
        $this->length = $length;
    }

    protected function shape()
    {
        return pow($this->length, 2);
    }
}

class Triangle implements Shape
{
    protected $base;

    protected $height;

    public function __construct(int $base, int $height)
    {
        $this->base = $base;
        $this->height = $height;
    }

    protected function getShape()
    {
        return .5 * $this->base * $this->height;
    }
}

Every class implements the Shape interface. The getShape method got no attributes. The attributes are protected properties of the class itself. You set these properties when calling the constructor of the specific class.

Marcel
  • 4,854
  • 1
  • 14
  • 24
1

I think how the idea to use the __construct is the best approach really. It is what that is for. You want each shape to be different and each shape has to calculate area differently. Hence polymorphism and OOP design principles.

With that said yes there are always other hacks. I do not recommend this approach but you can use it if wanted. Essentially pass in an array with keys for the pieces you want and use them.

abstract class Shape {
    abstract protected function getArea($data = null); //Default to null incase it is not passed.
}

class Square extends Shape {

    public function getArea($data) //$data should have a length key
    {
        if(isset($data)){
            return pow($data['length'], 2);
        }
    }

}

class Triangle extends Shape {

    public function getArea($data) //$data should have a base and height key
    {
        if(isset($data)){
            return .5 * $data['base'] * $data['height'];
        }
    }

}
nerdlyist
  • 2,842
  • 2
  • 20
  • 32