note 75031 added to language.oop5.magic

From: Date: Wed, 09 May 2007 10:09:22 +0000
Subject: note 75031 added to language.oop5.magic
Groups: php.notes 
Request: Send a blank email to php-notes+get-126358@lists.php.net to get a copy of this message
There is a trick to store static attributes with the objects in the $_SESSION array: <? session_start(); class Classes{ private $name; private $statics; function __construct($name){ $this->name=$name; $this->statics=array(); } function setStatic($key,$value){ if(!is_resource($value)) $this->statics[$key]=$value; } function __wakeup(){ foreach($this->statics as $k=>$v) eval($this->name."::\$".$k."=\$this->statics['".$k."'];"); unset($this); } } class Object{ function __sleep(){ $name=get_class($this); if(empty($_SESSION["_classes"])) $_SESSION["_classes"]=array(); if(empty($_SESSION["_classes"][$name])){ $_SESSION["_classes"][$name]=new Classes($name); $from_object=get_object_vars($this); $from_class=get_class_vars($name); if(count($from_object)>0) foreach($from_object as $k=>$value) if($clave=array_search($k,$from_class)) unset($from_class[$clave]); if(count($from_class)>0) foreach($from_class as $k=>$v) $_SESSION["_classes"][$name]->setStatic($k,$v); } return $from_object; } } ?> You must inherit all your objects from Object and when you save them in the session the static method from their classes will be store and restore to their original state. You must have one object from each class (that has static attributes) stored in the session. ---- Server IP: 130.206.1.5 Probable Submitter: 80.33.123.53 (proxied: 1.0 SIGASERVER) ---- Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php Edit -- https://master.php.net/note/edit/75031 Del: integrated -- https://master.php.net/note/delete/75031/integrated Del: useless -- https://master.php.net/note/delete/75031/useless Del: bad code -- https://master.php.net/note/delete/75031/bad+code Del: spam -- https://master.php.net/note/delete/75031/spam Del: non-english -- https://master.php.net/note/delete/75031/non-english Del: in docs -- https://master.php.net/note/delete/75031/in+docs Del: other reasons-- https://master.php.net/note/delete/75031 Reject -- https://master.php.net/note/reject/75031 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#126358) next »