note 75031 deleted from language.oop5.magic by felipe

From: Date: Sun, 30 Dec 2007 21:42:42 +0000
Subject: note 75031 deleted from language.oop5.magic by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-134552@lists.php.net to get a copy of this message
Note Submitter: alejandro dot gama at gmail dot com ---- 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.

« previous php.notes (#134552) next »