2
I’m doing a study of Project Standards. My first standard is Strategy, where I have an abstract class that has two attributes that should receive instances of a class that implements a certain 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 is these classes of this type that should be assigned to the $flyBehavior and $quackBehavior attributes and I would like to state it as follows:
abstract class Duck
{
protected FlyBehavior $flyBehavior;
protected QuackBehavior $quackBehavior;
}
But if I do this the editor accuses error. How can I do it? Is this wrong? Because I know that you can specify the type of variable in function parameters, I imagine it is also possible in attributes, as well as in languages like Java.