2015-01-21 2 views
1

두 개의 PHP 클래스가 있습니다. &PHP에서 클래스를 사용하여 두 개 이상의 함수를 결합하는 경우

Class classTwo extends classOne { 

    private $stuff; 
    public $stuff2; 

    public function init(){ #This function is overriding the native classOne method init; 
    dosomeotherstuff; 
    } 

} 

난 함수 초기화에게 전화

Class classOne { 

    private $stuff; 
    public $stuff2; 

    public function init(){ 
    dosomestuff; 
    } 

} 

$obj = new classTwo(); 
$obj -> init(); #dosomeotherstuff 

PHP 인터프리터 classTwo 클래스는 방법에 대한 재정의를 선언하기 때문에 사람이 기대하는 것처럼 dosomeotherstuff 것 초기화;

대신 첫 번째 init과 두 번째 효과를 결합하여 다음과 같은 결과를 얻는 방법이 있습니까? childre 방법에

public function init() { 
    parent::init(); 
    // your normal code 
} 
+0

그냥 당신이 전화를해야 init 메소드 (부모 :: init();)를 seconde classe init 메소드 –

+0

솔직히, [매뉴얼 읽기] (ht tp : //php.net/manual/en/keyword.parent.php). 제발 ... 숙제 해. 결국 [연구를 요청] (http://stackoverflow.com/help/how-to-ask). 추신 : 올바른 용어는 메소드 _overriding_, _overloading_은 완전히 다른 것입니다 –

+0

@EliasVanOotegem 사과드립니다. 다른 섹션에서 올바른 용어를 사용했음을 알 수 있습니다. – Ivan

답변

3

당신은 기본 함수를 호출 할 수 있습니다

Class classTwo extends classOne { 

    private $stuff; 
    public $stuff2; 

    public function init(){ #This function is overloading the native classOne method init; 
    parent::init(); 
    dosomeotherstuff; 
    } 

} 
1

사용 부모 : 오버라이드 기능에서

$obj = new classTwo(); 
$obj -> init(); #dosomestuff, #dosomeotherstuff 

정말 감사합니다

관련 문제