在PHP OOP中,可以通过外部引用该对象来调用对象的父级方法?PHP OOP如何从对象本身调用对象的父方法?
Class ObjectOne {
protected function method() {
// Does something simple
}
}
Class ObjectTwo extends ObjectOne {
protected function method() {
$temp = clone $this;
$this->change_stuff();
if(parent::method()) {
// Do more stuff here
$temp->method();
// This will call this method, not the parent's method.
}
}
protected function change_stuff() {
// Change this object's stuff
}
}
我不能调用父::()的方法,因为这将导致当前对象执行它的方法。 我想要$ temp的代替。
解决
我解决它写的是从类中调用parent::update()
方法的另一个功能:
Class ObjectOne {
protected function method() {
// Does something simple
}
}
Class ObjectTwo extends ObjectOne {
protected function method() {
$temp = clone $this;
$this->change_stuff();
if(parent::method()) {
// Do more stuff here
$temp->update_parent();
// This will call this method, not the parent's method.
}
}
protected function change_stuff() {
// Change this object's stuff
}
protected function update_parent() {
return parent::update();
}
}
我无法调用'parent :: update()',因为这会导致**当前对象**将其属性保存到数据库中。我想要$ temp的代替。 –
不,它不会。它会调用父对象的'update()'方法,如果你做'parent :: update()' – u54r
你错过了这一点。我不希望这个对象调用update()方法,无论他或父母的。我需要$ temp来调用它。 –