Php 覆盖原则魔术存取器方法
我尝试重写一个属性getter方法(由sfDoctrineRecord::\uu call()方法处理),如下所示:Php 覆盖原则魔术存取器方法,php,symfony1,doctrine,Php,Symfony1,Doctrine,我尝试重写一个属性getter方法(由sfDoctrineRecord::\uu call()方法处理),如下所示: //myClass.class.php public function getProperty() { $property = parent::getProperty(); //the following line is never reached return $property; } public function getProperty() { $prop
//myClass.class.php
public function getProperty()
{
$property = parent::getProperty();
//the following line is never reached
return $property;
}
public function getProperty()
{
$property = $this->_get('property');
//the following line is never reached
return $property;
}
但这会导致无限递归。它是否可能以及如何实现?在方法中,您将看到它使用调用用户函数数组,该数组将尝试调用类的getProperty
方法
由于您已经重写了getProperty
,它正在调用子类定义,因此它正在调用自己。在方法中,您将看到它使用call\u user\u func\u array
,它将尝试调用类的getProperty
方法
因为您已经重写了getProperty
,所以它正在调用子类定义,所以它正在调用自己。尝试如下操作:
//myClass.class.php
public function getProperty()
{
$property = parent::getProperty();
//the following line is never reached
return $property;
}
public function getProperty()
{
$property = $this->_get('property');
//the following line is never reached
return $property;
}
另外,请阅读有关自定义变异器和访问器的内容。尝试以下方法:
//myClass.class.php
public function getProperty()
{
$property = parent::getProperty();
//the following line is never reached
return $property;
}
public function getProperty()
{
$property = $this->_get('property');
//the following line is never reached
return $property;
}
另外,请阅读有关自定义变量和访问器的内容。如果parent::getProperty()
方法正在使用\u调用方法,那么是的,重写getProperty
将导致parent::getProperty()
反复调用子方法。我认为您需要在子类中复制父getProperty方法的功能来实现这一点。但是,如果您的子类的getProperty
方法像上面显示的那样简单,那么您根本不需要重写它。家长的版本会做你想做的。不,当然不是,我要应用一些逻辑。感谢您的评论,通过直接调用_get()方法解决了这个问题:$property=$this->\u get('property')。如果parent::getProperty()
方法正在使用\u call
方法,那么是的,重写getProperty
将导致parent::getProperty()
反复调用子方法。我认为您需要在子类中复制父getProperty方法的功能来实现这一点。但是,如果您的子类的getProperty
方法像上面显示的那样简单,那么您根本不需要重写它。家长的版本会做你想做的。不,当然不是,我要应用一些逻辑。感谢您的评论,通过直接调用_get()方法解决了这个问题:$property=$this->_get('property'))