开发者

How to keep method calls in base class stick to base class in PHP?

开发者 https://www.devze.com 2023-02-23 01:11 出处:网络
class son extends parent { ... public function func_name() { //do some additional stuff here ... parent:func_name();
class son extends parent {
    ...
    public function func_name()
    {
        //do some additional stuff here
        ...
        parent:func_name();
    }

}

But in parent there's another method:

class parent {
...
开发者_开发技巧    public another_func()
    {
        $this->func_name();//how to stick to the one in parent here???
    }
}

Example:

$inst = new son;
$inst->another_func()////how to make the func_name within another_func stick to the one in parent???


Rename parent::func_name and make it private. Call that function from parent::another_func (and possibly from the new implementation of parent::func_name).


public another_func () {
    if (get_class($this) == 'parent') {
        $this->func_name(); // $this is an instance of parent object
    } else {
        parent::func_name(); // $this is an instance of some child class
    }
}


Or wouldn't ParentClassName::func_name work?

0

精彩评论

暂无评论...
验证码 换一张
取 消