2012-06-09 2 views
0

나는 "WORKED"를 반환 할 것으로 기대되는 코드를 가지고 있지만 아무것도 반환하지 않습니다.클래스 계층

class Foo { 
    public function __construct() { 
     echo('Foo::__construct()<br />'); 
    } 

    public function start() { 
     echo('Foo::start()<br />'); 

     $this->bar = new Bar(); 
     $this->anotherBar = new AnotherBar(); 
    } 
} 

class Bar extends Foo { 
    public function test() { 
     echo('Bar::test()<br />'); 

     return 'WORKED'; 
    } 
} 

class AnotherBar extends Foo { 
    public function __construct() { 
     echo('AnotherBar::__construct()<br />'); 

     echo($this->bar->test()); 
    } 
} 

$foo = new Foo(); 
$foo->start(); 

라우터 :

Foo::__construct() <- From $foo = new Foo(); 
Foo::start() <- From Foo::__construct(); 
Foo::__construct() <- From $this->bar = new Bar(); 
AnotherBar::__construct() <- From $this->anotherBar = new AnotherBar(); 

내가 Foo 클래스에서 $bar을 정의하고, FooAnotherBar, 나는 이미 Foo에서 정의 된 변수를 얻을 것으로 예상 확장 때문에

.

나는 무엇이 잘못되었는지를 볼 수 없습니다. 어디에서 캠 시작합니까?

감사합니다.

답변

3

AnotherBar 인스턴스의 start 메서드가 호출되지 않아 해당 $this->bar이 정의되지 않았습니다. 물론

ini_set('display_errors', 'on'); 
error_reporting(E_ALL); 

:

Notice: Undefined property: AnotherBar::$bar in - on line 20 
Fatal error: Call to a member function test() on a non-object in - on line 20 

당신은 모든 오류를보고 바로 당신이 <?php 라인 뒤에 다음 코드를 포함 할 수 있습니다 오류와

는 다음과 같은 메시지를 얻을 표시되는 더 깨끗한 해결책이 될 php.ini을 통해이 작업을 수행 할 수도 있습니다.

+0

예,이 오류에 대해 알고 있습니다. 내 질문이 업데이트 될 것입니다. –

+0

질문이 업데이트되었습니다. –

관련 문제