2016-09-17 5 views
-6

나는 2 개의 클래스, BeerIngredient을 얻었습니다.속성으로 객체, 부모 클래스 얻기

내 클래스 Beer은 이라는 속성이 있습니다. 배열은 Ingredient입니다.

만약이 객체가 에 의해 인스턴스화, 또는 Beer$ingredients 재산에 속하는 경우 내 Ingredient 수업 시간에 내가 결정할 수있는 방법은?

+1

"인스턴스화 된 _hand_"은 무엇을 의미합니까? – SOFe

+0

내 코드에는 다음과 같은 의미가 있습니다. $ ingredients = new Ingredient (2); – Treast

+0

잠깐, 생성자를 사용하는 것 외에는 '성분'을 만드는 다른 방법이 있습니까? – SOFe

답변

0

암시 적 메서드가 없습니다. 인스턴스가 발생하면 인스턴스에 알려야합니다. Ingredient의 인스턴스가 $ingredient->hasOwner()를 사용하여 Beer에 사용하는 경우

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); 
    } 
} 

지금 당신이 확인할 수 있습니다

이 예를 참조하십시오.