2013-08-01 5 views
4

나는 (추상) 부모 클래스가 건설 중에 기능을 제공한다고 가정한다. 하위 클래스는 생성자에서 사용되는 속성을 재정의 할 수PHP 상속 : 생성자에서 사용하기위한 부모 변수/속성을 재정의하는 자식 클래스

class Parent extends MiddlewareTest 
{ 
    // abstract channel properties 
    protected $title = NULL; 
    protected $type = NULL; 
    protected $resolution = NULL; 

    function __construct() { 
     parent::__construct(); 

     $this->uuid = $this->createChannel($this->title, $this->type, $this->resolution); 
    } 
} 

class Child extends Parent 
{ 
    // channel properties 
    protected $title = 'Power'; 
    protected $type = 'power'; 
    protected $resolution = 1000; 
} 

문제는 실행을 무시하지 않는 Child::__construct() ($this->createChannelNULL 매개 변수라고 함) 할 때 오버라이드 (override) 속성이 사용되지 않는 것입니다.

PHP로 가능합니까? 아니면 원하는 기능을 제공 할 때마다 하위 생성자를 재정의해야합니까?

참고 : Properties shared between child and parents class in php을 보았습니다. 그러나 이것은 자식 속성이 생성자에서 할당되지 않고 정의에 따라 다르기 때문에 다릅니다.

업데이트

그것은 내 테스트 케이스가 문제가있는 것 밝혀졌습니다. MiddlewareTest가 SimpleTest 유닛 테스트 케이스를 기반으로했기 때문에 SimpleTest는 실제로 내가 모르는 부분을 가지고있었습니다. 자동 실행은 결코 의도되지 않은 Parent 클래스 자체를 인스턴스화하기 때문입니다. Parent 클래스를 추상화하여 수정되었습니다.

학습 : 깨끗한 테스트 사례를 작성하고 실제로 울기 전에 실행하여 도움을 요청하십시오.

답변

2

서버에서 어떻게 발생하는지 잘 모르겠습니다. 나는 MiddlewareTest 클래스에 대한 가정이, 당신의 클래스 이름을 수정했습니다, 그리고 몇 가지 간단한 디버깅 라인을 추가하지만,이 코드 :

37: array (
    0 => 'Power', 
    1 => 'power', 
    2 => 1000, 
) 

20: array (
    0 => 'Power', 
    1 => 'power', 
    2 => 1000, 
) 

21: array (
    0 => 'Power', 
    1 => 'power', 
    2 => 1000, 
) 

39: 'array (
    0 => \'Power\', 
    1 => \'power\', 
    2 => 1000, 
)' 

당신이 볼 수 있듯이 :

<?php 
/** 
* I'm not sure what you have in this class. 
* Perhaps the problem lies here on your side. 
* Is this constructor doing something to nullify those properties? 
* Are those properties also defined in this class? 
*/ 
abstract class MiddlewareTest { 
    // I assume this properties are also defined here 
    protected $title = NULL; 
    protected $type = NULL; 
    protected $resolution = NULL; 
    protected $uuid = NULL; 

    public function __construct() 
    {} 

    protected function createChannel($title, $type, $resolution) 
    { 
     echo "<pre>" . __LINE__ . ": "; var_export(array($this->title, $this->type, $this->resolution)); echo "</pre>"; 
     echo "<pre>" . __LINE__ . ": "; var_export(array($title, $type, $resolution)); echo "</pre>"; 
     return var_export(array($title, $type, $resolution), true); 
    } 
} 

// 'parent' is a keyword, so let's just use A and B 
class A extends MiddlewareTest 
{ 
    // abstract channel properties 
    protected $title = NULL; 
    protected $type = NULL; 
    protected $resolution = NULL; 

    function __construct() { 
     parent::__construct(); 

     echo "<pre>" . __LINE__ . ": "; var_export(array($this->title, $this->type, $this->resolution)); echo "</pre>"; 
     $this->uuid = $this->createChannel($this->title, $this->type, $this->resolution); 
     echo "<pre>" . __LINE__ . ": "; var_export($this->uuid); echo "</pre>"; 
    } 
} 

class B extends A 
{ 
    // channel properties 
    protected $title = "Power"; 
    protected $type = "power"; 
    protected $resolution = 1000; 
} 

$B = new B(); 
?> 

나는이 결과를 얻을 수 값은 예상대로 클래스가 인스턴스화 된 것처럼 정의 된대로 전달됩니다.

MiddlewareTest 클래스에서이 동작이 발생하는 이유에 대해 자세히 설명해 줄 수 있습니까?

실행중인 PHP 버전은 무엇입니까?

+0

실례를 제공하지 않아 죄송합니다. 당신의 힌트가 건물 하나에 시작하게하고 당신의 요점을 볼 수 있습니다. 차이점을 지금 찾으려고 노력 중입니다. – andig