現在、クラスを拡張して機能を追加する必要があります (基本クラスにアクセスして変更することはできません)。問題が発生しています。
基本的に、要求された場合はプライベート変数のセットを返す魔法のゲッター関数が必要ですが、それ以外の場合はデフォルトの動作にデフォルト設定されます。マジック セッター関数を使用して一部のデータを自動的に同期するには、これらのプロパティを非公開にする必要があります。
とはいえ、ここにいくつかのサンプルコードがあります:
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 関数を使用できることはわかっていますが、そのような操作を実行することは直感に反するという議論にもかかわらず、これをできるだけ直感的なものにしたいと考えています。2 つの私有財産は互いに密接に関連しています。それらの1つが設定されると、論理的に他のものに影響を与えます。
現時点では、これが getter/setter 関数を回避し、プロパティ間の密接な同期を維持するために考えられる唯一の論理的な方法です。他に実行可能な解決策が思いつく場合は、お気軽にオプションを提案してください:)