Friday, 15 April 2011

PHP - serialize a class with static properties -



PHP - serialize a class with static properties -

when user logs site, create instance of user class, fetch user-related info , store object in session.

some of info fetch database should constant throughout session , want info accessible other objects. prefer using user::$static_value_in_class $_session['static_value_in_session'] when using value within object, i'm open persuasion.

the problem is, values aren't remembered when serialize user instance session, load different page.

class definitions:

class user { public $name; public static $allowed_actions; public function __construct($username, $password) { // validate credentials, etc. self::$allowed_actions = get_allowed_actions_for_this_user($this); } } class blog { public static function write($text) { if (in_array(user_may_write_blog, user::$allowed_actions)) { // write blog entry } } }

login.php:

$user = new user($_post['username'], $_post['password']); if (successful_login($user)) { $_session['user'] = $user; header('location: index.php'); }

index.php:

if (!isset($_session['user'])) { header('location: login.php'); } blog::write("i'm in index.php! hooray!") // won't work, because blog requires user::$allowed_actions

should implement serializable , write own version of serialize() , unserialize() include static data?

should bite lip , access $_session variable within blog class?

should require valid user instance sent blog write() method?

or maybe internets has improve idea...

edit: writing real utilize case (not total code, plenty gist).

my site handles groups of users shared budget accounts. users may spend grouping money on things grouping agreed upon, , study transactions creating instances of transaction class , sending bank class database storage.

bank class:

class bank { // group-agreed reasons spend money public static $valid_transaction_reasons; public function __construct(user $user) { bank::$valid_transaction_reasons = load_reasons_for_this_group($user->bank_id); } }

user class:

class user { public $bank_id; public function __construct($username, $password) { $query = "select bank_id users username=$username , password=$password"; $result = mysql_fetch_array(mysql_query($query)); $this->bank_id = $result['bank_id']; } }

transaction class:

class transaction { public function __construct($reason, $amount) { if (!in_array($reason, bank::$valid_transaction_reasons)) { // error! users can't spend money on this, grouping doesn't cover } else { // build transaction object } } }

actual code (login.php, or something):

$user = new user($_get['uname'], $_get['pword']); $_session['bank'] = new bank($user); // shit happens, user navigates submit_transaction.php $trans = new transaction(reason_beer, 5.65); // error! bank::$valid_transaction_reasons empty!

as mentioned in comment, more software design question question how accomplish php.

a static property not part of state of object , hence not beingness serialized it.

i'll give short illustration how solve related problem. imagine have next message class, has static $id property create sure instances have unique id:

class message { public static $id; public $instanceid; public $text; /** * */ public function __construct($text) { // id incremented in static var if(!self::$id) { self::$id = 1; } else { self::$id++; } // create re-create @ current state $this->instanceid = self::$id; $this->text = $text; } }

serialization / unserialization code:

$m1 = new message('foo'); printf('created message id: %s text: %s%s', $m1->instanceid, $m1->text, php_eol); $m2 = new message('bar'); printf('created message id: %s text: %s%s', $m2->instanceid, $m2->text, php_eol); $messages = array($m1, $m2); $ser1 = serialize($m1); $ser2 = serialize($m2); $m1 = unserialize($ser1); printf('unserialized message id: %s text: %s%s', $m1->instanceid, $m1->text, php_eol); $m2 = unserialize($ser2); printf('unserialized message id: %s text: %s%s', $m2->instanceid, $m2->text, php_eol);

to create sure id unique across multiple script runs farther work nessary. you'll have create sure message::$id initialized before object creation, using value lastly script run. additionally wired when comes parallel php request on webserver.

its illustration simplest static property know: instance counter. in case so. hope see there farther work required serialize / unserialize static properties without have side effects. , depends on application needs.

this question cannot answered general tend makes no sense in case serialize static members. appreciate comments on this.

php class serialization static

No comments:

Post a Comment