2011-12-27 3 views
1

현재 모든 다른 컨트롤러와 모델을로드하는 기본 클래스가 Base이고 Base에 의해로드 된 각 클래스는 비슷한 구조를가집니다. :

class SomeClass { 

    private $base; 

    function __construct(&$base) { 
     $this->base = $base; 
    } 

    function SomeMethod() { } 

} 

그런 다음 다른 클래스가 사용하는 것 : 이러한 다른 클래스를 액세스하는 더 좋은 방법은

class AnotherClass { 

    private $base; 

    function __construct(&$base) { 
     $this->base = $base; 

     $this->base->SomeClass->SomeMethod(); 
    } 

} 

있습니까?

+0

SomeClass가 BaseClass를 확장 한 클래스를 보았습니까? –

+0

BaseClass의 기능을 확장하지 않습니다. 그것들은 특정 목적을 위해 BaseClass에 의해 호출 된 후에로드됩니다. 예. BaseClass는 DatabaseClass와 AuthenticationClass를 호출 한 다음 AuthenticationClass와 DatabaseClass를 사용하는 Controll SomeController를 호출 할 수 있습니다. – jSherz

+0

죄송합니다. 본인의 원래 질문을 정확하게 이해하지 못했던 것처럼 보입니다. 이전 의견을 무시하십시오. –

답변

1

아마도 것으로 someMethod()는 정적 일 수 있습니다

: 바로 다음

class SomeClass { 

    private $base; 

    function __construct(&$base) { 
     $this->base = $base; 
    } 

    public static function SomeMethod() { } 

} 

+0

조언 해 주셔서 감사합니다. 이것은 수업을 사용해야하는 모든 상황에서는 작동하지 않지만 몇 가지 경우에는 효과가 있습니다. – jSherz

1

BaseFront Controller pattern의 구현입니다. 프론트 컨트롤러는 정확히 당신이하고있는 일을 수행하는 Mediator의 특별한 케이스입니다. 기본적으로 SomeClassAnotherClass을 적은 수의 종속성으로 별도로 개발하고 유지 관리 할 수 ​​있습니다.

그러나, 오히려 직접 Base 클래스에서 클래스를 액세스하는 것보다, 그것은 SomeClass을 가지고 AnotherClassBase 클래스에 자신을 등록하고 다른 개체 전화 getter 메소드를 노출하는 것이 가장 수 있습니다 :

class Base { 
    protected $_authenticator; 

    public function setAuthenticator(Authenticator $auth) { 
     $this->_authenticator = $auth; 
    } 

    public function getAuthenticator() { 
     return $this->_authenticator; 
    } 
} 

class Authenticator { 
    protected $_base; 

    public function __construct(Base $base) { 
     $this->_base = $base; 
     $this->_base->setAuthenticator($this); 
    } 
} 
+0

메모 해 주셔서 감사합니다. 내 프로젝트에 이것을 구현하는 데 갈 것입니다. – jSherz