2013-10-25 3 views
0

의 속성에 액세스하지 못합니다.이 코드가 있습니다. 자식 클래스의 멤버 변수를 활용하는 상속 된 메서드를 가져 오려고합니다.추상 멤버 함수가 PHP 상속 클래스

abstract class HtmlObj{ 
//abstract protected function jQuery_Activity(); 
public $hyperlink; 
abstract protected function php_Activity(); 
abstract protected function print_Widget(); 

function __construct($hyperlink=""){ 
    if(isset($hyperlink)){ 
     $this->hyperlink = $hyperlink; 
    } 
    $this->php_Activity(); 
    $this->Print_Widget(); 
} 

}

class child extends HtmlObj{ 
    public $id; 
    protected function php_Activity(){return;} 
    protected function print_Widget(){ 
     print $this->id; 
    } 
    function __construct($id){ 
    this->id = $id; 
    } 
} 

불행하게도이 아무것도 출력하지 않습니다. 왜 그런가? 나를 위해 작동 -

답변

1

는 자식 클래스에서 당신은

abstract class HtmlObj 
{ 
//abstract protected function jQuery_Activity(); 
    public $hyperlink; 

    abstract protected function php_Activity(); 

    abstract protected function print_Widget(); 

    function __construct($hyperlink = "") 
    { 
     if (isset($hyperlink)) { 
      $this->hyperlink = $hyperlink; 
     } 
     $this->php_Activity(); 
     $this->Print_Widget(); 
    } 
} 

class child extends HtmlObj 
{ 
    public $id; 

    protected function php_Activity() 
    { 
     return; 
    } 

    protected function print_Widget() 
    { 
     print $this->id; 
    } 

    function __construct($id) 
    { 
     $this->id = $id; 
     parent::__construct(); 
    } 
} 

new child(10); 
+0

나는 전체 코드를 붙여 넣은 같은 작업을 수행하여 :: __ 구조() 부모에게 reffer해야합니다. –

+0

고마워요! 왜 처음에는 작동하지 않았는지 모르지만 두 번째로 작동했습니다 ... –