我目前需要扩展一个类以向它添加功能(我无权访问基类来修改它),并且我遇到了它的问题。
基本上,如果需要,我需要魔术 getter 函数来返回一组私有变量,否则默认为默认行为。我需要这些属性是私有的,以便使用魔法设置器功能自动同步一些数据。
也就是说,这里有一些示例代码:
class newClass extends baseClass {
private $private1;
private $private2;
...
public function __get($name) {
if($name == 'private1') return $this->private1;
if($name == 'private2') return $this->private2;
... (and so on)
// and here, it should default back to it's default behavior (throwing
// an error on getting invalid/inaccessable property, etc.)
// I cannot just use property_exists, because there may or may not be
// private variables in the base class that should not be exposed.
}
public function __set($name,$val) {
// I use this to do some automatic syncing when the two private variables
// above are set. This needs to be triggered, hence the private variables
// in the first place.
}
}
我知道,我可以使用 getProperty/setProperty 函数,但我希望它尽可能保持直观,尽管有人认为执行此类操作是违反直觉的。这两个私有财产彼此非常相关。当其中一个被设置时,它在逻辑上会影响其他人。
到目前为止,这是我能想到的避免 getter/setter 函数并保持属性之间紧密结合的同步的唯一合乎逻辑的方法。如果你们能想到任何其他可行的解决方案,请随时提出选择:)