I am doing a study of Project Patterns. My first pattern is Strategy, where I have an abstract class that has two attributes that should instantiate a class that implements a given interface.
The interfaces:
interface FlyBehavior
{
public function fly();
}
interface QuackBehavior
{
public function quack();
}
The abstract class:
abstract class Duck
{
protected $flyBehavior;
protected $quackBehavior;
abstract function display();
protected function performFly()
{
return $this->flyBehavior->fly();
}
protected function performQuack()
{
return $this->quackBehavior->quack();
}
}
The point is this: I have some classes that implement FlyBehavior and QuackBehavior, and it's these classes of this type that should be assigned to the $ flyBehavior and $ quackBehavior attributes and I'd like to declare it as follows:
abstract class Duck
{
protected FlyBehavior $flyBehavior;
protected QuackBehavior $quackBehavior;
}
However, if I do this, the publisher will acknowledge the error. How can I do it? This is wrong? I know that you can specify the type of variable in function parameters, I imagine it is also possible in attributes, as in languages like Java.