How to keep method calls in base class stick to base class in PHP?
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?
精彩评论