他の言語の便利な機能の 1 つは、プロパティの get メソッドと set メソッドを作成できることです。この機能を PHP で複製する良い方法を見つけようとして、私はこれに出くわしました: http://www.php.net/manual/en/language.oop5.magic.php#98442
そのクラスの内訳は次のとおりです。
<?php
class ObjectWithGetSetProperties {
public function __get($varName) {
if (method_exists($this,$MethodName='get_'.$varName)) {
return $this->$MethodName();
} else {
trigger_error($varName.' is not avaliable .',E_USER_ERROR);
}
}
public function __set($varName,$value) {
if (method_exists($this,$MethodName='set_'.$varName)) {
return $this->$MethodName($value);
} else {
trigger_error($varName.' is not avaliable .',E_USER_ERROR);
}
}
}
?>
私の計画は、このクラスを拡張し、この拡張クラスで適切なget_someproperty()
andを定義することでした。set_someproperty()
<?php
class SomeNewClass extends ObjectWithGetSetProperties {
protected $_someproperty;
public function get_someproperty() {
return $this->_someproperty;
}
}
?>
問題は、 の基底クラスが のObjectWithGetSetProperties
メソッドを認識できないことget_someproperty()
ですSomeNewClass
。「キーが利用できません」というエラーが常に表示されます。
これを解決して、の基本クラスが機能するようにする方法はありますか、または各クラスでそれらと魔法のメソッドObjectWithGetSetProperties
を作成する必要がありますか?__get()
__set()