dongpai9986 2012-07-13 09:14
浏览 132
已采纳

当A类扩展B类时,它们都具有相同的属性

I have class A and class B. where class A extends class B.

They both have the property $fields, where $fields is an array, like:

class A fields

public $fields = array( 'id'=>'', 'product'=>'', 'productXpath'=>'', 'price'=>'', 'priceXpath'=>'', 'currency'=>'', 'website_url'=>'', 'url_id'=>'', 'day'=>'', 'month'=>'', 'year'=>'', 'time'=>'', 'status'=>'' );

class B fields

public $fields = array( 'id'=>'', 'website'=>'', 'visits'=>'', 'plugin_id'=>'', 'status'=>'' );

Only the structure and values within the array are different.

I need to access both properties, how do i do this ?

  • 写回答

2条回答 默认 最新

  • duanjianxu4288 2012-07-13 09:17
    关注

    Edit:

    If you have control over class B, simply define a getter and make $fields private:

    public function getFields() {
        return $this->fields;
    }
    

    Then in class A you can do:

    public function getFields() {
        $parentFields = parent::getFields();
        // Do something with $parentFields
        return $this->fields;
    }
    

    If not, you'll have to give a different name to the $fields property in class A, so as not to lose the value of class B.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?