[Solved] Object as attribute, get parent class


No implicit methods. You must notify the instance when that happens.

See this example:

class Beer{
    private $ingredients = [];

    public function addIngredient(Ingredient $ing){
        $this->ingredients[] = $ing;
        $ing->setOwner($this);
    }
}

class Ingredient{
    private $owner;

    public function getOwner(){
        return $this->owner;
    }

    public function setOwner($owner){
        $this->owner = $owner;
    }

    public function hasOwner() : bool{
        return isset($this->owner);
   }
}

Now you can check if an instance of Ingredient is used in a Beer using $ingredient->hasOwner().

solved Object as attribute, get parent class