2

I found myself in this situation and I just hacked it dirtily. But I wonder, is there some clean way?

class base{
    var $value;
    public function getValue(){
        return 'returned from base: '. $value;
    }
}

class object extends base{
    public function getValue(){
        $this->value = 'wrong value';
        return parent::getValue();
    }
}


class overwrite extends object{
    public function getValue(){
        $this->value = 'right value';

        ?????

    }
}

$o = new overwrite;
echo $o->getValue();

All I'm allowed to edit is overwrite. If I call parent::getValue(), the value gets overwritten by wrong value. If I name the class, like base::getValue(), the base class would not have $value property set.

4

1 回答 1

1

我在完成问题之前就想到了这一点,但我还是决定发布它:

base::getValue() 似乎是正确的解决方案,php 似乎在那里做了一些魔术,所以当从其子级调用时,base类具有属性集。$value

于 2013-05-15T11:58:03.037 回答