データベースの列を異なる名前のインスタンス フィールドにマップできる軽量の ORM を作成しています。
例えば。
データベース
- ユーザーID
- ユーザー名
- 別の不適切な名前のフィールド
物体
- ユーザーID
- ユーザー名
- another_poorly_named_field
これを行うために、私の元のデザインは次のようなものでした
abstract class DbObj {
/* In a subclass the author would provide the mapping of fileds
* from the database to the object
* e.g.
* array('userid' => 'user_id', 'username' => 'username'....
*/
protected static $db_to_obj = array();
/* This would be auto popuplated from $db_to_obj in reverse */
protected static $obj_to_db = array();
/* Many Methods truncated... */
/* Used by magic methods to make sure a field is legit */
public function isValidField($name) {
return in_array(strtolower($name), self::$db_to_obj);
}
}
次に、これをサブクラス化します
class Cat extends DbObj {
protected static $db_to_obj = array(
'catsname' => 'name',
'itsage' => 'age'
);
}
isValidField
メソッドが期待どおりに機能しません。デバッガーまたは古き良きものを使用すると、 の値が親クラスvar_dump
の値であることがわかります。self::$db_to_obj
だったら理解できますisValidField
がstatic
、そうではありません。ポインターが$this
あり、そのクラスを知っています。
この動作の回避策または使用するより良いアーキテクチャはありますか?