これは私が少しうるさいですが、簡単に機能するのが本当に好き$_SESSION
です。そのため、データベース内の行をそのように機能させる方法があるかどうか疑問に思っていました。スーパーグローバルを作成するのは簡単だったので、たとえば、$_DATA['address']
現在ログインしているユーザーのデータベースに保存されているアドレスを返します。明らかな問題は、それに何かを書き込むと$_DATA['whatever']
、自動的にデータベースに書き込まれることです。これは、私が慣れ親しんでいる C# では簡単ですが、PHP では通常の get/set 機能がないようです。私がやりたいことを達成する方法はありますか?
2 に答える
1
クラスを作成し、いくつかの静的ヘルパー関数を与えることができます
例えば:
class CurrentUser {
protected static $currentUser;
protected static function getCurrentUser(){
if (!static::$currentUser){
// get the current user from db and assign it to the currentUser Property
}
return static::$currentUser;
}
public static function get($property){
return isset(static::getCurrentUser()->$property)?static::$currentUser->$property:null;
}
public static function set($property, $value){
// make sure we have the current user
$user = static::getCurrentUser();
if ($user){
$user->$property = $value;
// save the user to the database.
}
}
}
使用するには、次のように言うだけです
echo CurrentUser::get("address");
echo CurrentUser::set("address", "123 anystreet, anytown US 12345");
于 2013-07-07T05:22:42.080 に答える