私のプログラムでは、オブジェクトはデータベースの1行を表し、すべて同じデータとセッターとゲッターなどを持っています.
各列の値は、オブジェクト内のプロパティに保存されます。例えば:
class Person extends Base {
protected $name;
protected $age;
/* Setters & Getters */
}
Base クラスには、Person クラスのプロパティから更新クエリを作成する Save() メソッドがあります。Base の Save() メソッドにプロパティを渡す方法がわかりません。心に留めていることはほとんどありませんが、あまり満足していません。
1. すべてのプロパティにプレフィックスを追加します。「db_」のように:
protected $db_id;
protected $db_age;
Loop get_object_vars($this) and check the prefix.
- 紛らわしい
- 醜い
2. データベース内の行に関連するすべてのプロパティ名を含む配列を追加します
protected $db_properties = array("id", "age");
protected $id;
protected $age;
Loop get_object_vars($this) and check if they are in the $db_properties array.
Or loop the $db_properties and get the properties.
- 二重同一データ
3. すべてのプロパティが常にデータベース内の行に関連付けられていると想定します。
Loop get_object_vars($this).
- 他のもののプロパティを追加できません
これを行う他の方法はありますか?