簡體   English   中英

PHP繼承:子類覆蓋構造函數中使用的父變量/屬性

[英]PHP inheritance: child class overriding parent variable/property for use in constructor

我有一個(抽象的)父類,應該在構造過程中提供功能。 子類可以覆蓋構造函數中使用的屬性:

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()時,不使用覆蓋的屬性(使用NULL參數調用$this->createChannel )。

這是否可能在PHP中實現?還是我每次都必須依靠重寫子構造函數來提供所需的功能?

注意:我看到了php的child和parent類之間共享的Properties,但這是不同的,因為child屬性不是在構造函數中分配的,而是根據定義。

更新資料

事實證明我的測試用例有問題。 由於MiddlewareTest基於SimpleTest單元測試用例,因此SimpleTest實際上-我沒有意識到-通過自動運行實例化了Parent類本身,而這從未意圖。 通過使Parent類抽象化來解決。

獲得的經驗教訓:構建一個干凈的測試用例並在哭泣之前實際運行它。

我不確定服務器上的情況如何。 我必須對MiddlewareTest類進行假設,修改您的類名稱,並添加一些簡單的調試行,但是要使用以下代碼:

<?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();
?>

我得到以下結果:

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,
)'

如您所見,這些值恰好像在實例化類中定義的那樣被傳入。

您能否在您的MiddlewareTest類上提供一些詳細信息,從而可以弄清為什么您會遇到這種行為?

您正在運行什么版本的php?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM