note 57540 added to language.oop5.autoload

From: Date: Thu, 06 Oct 2005 20:31:46 +0000
Subject: note 57540 added to language.oop5.autoload
Groups: php.notes 
Request: Send a blank email to php-notes+get-96528@lists.php.net to get a copy of this message
somebody might find this "quick and dirty" example useful (see the code below). BTW, is there a magic metod that tells you what the class file name is? If none, perhaps a feature request? What this example about: store objects in the session and restore them without worry of what external files to be "required" (solves the problem when you get incomplete PHP object, when your class is not declared prior unserialize()) PHP5, but easy to adapt for PHP4 --- file Session.php <?php session_start(); // don't do any comments, nothing special in this class IMO class Session { public static function has($name) { return isset($_SESSION[$name]); } public static function get($name) { if(Session::has($name)) { if(Session::has('__requireFiles')) { $requireFiles = unserialize($_SESSION['__requireFiles']); if(array_key_exists($name, $requireFiles)) { require_once($requireFiles[$name]); } } return unserialize($_SESSION[$name]); } return null; } public static function put($name, $value) { if(is_object($value)) { if(method_exists($value, 'getClassFile')) { if(!($requireFiles = Session::get('__requireFiles'))) $requireFiles = array(); $requireFiles[$name] = $value->getClassFile(); Session::put('__requireFiles', $requireFiles); } } $_SESSION[$name] = serialize($value); } } ?> --- end Session.php --- file a.php <?php include('Session.php'); // an object, all as usuall, but with one magic method // getClassFile(), you can call it whatever you like class A { public $a; function __construct($a) { $this->a = $a; } // our kinda magic method public function getClassFile() { return __FILE__; } } $a = new A('test'); Session::put('a', $a); } ?> --- end a.php --- file b.php <?php include('Session.php'); // everybody's happy :) $a = Session::get('a'); ?> --- end b.php ---- Manual Page -- http://www.php.net/manual/en/language.oop5.autoload.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57540 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57540&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57540&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57540&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57540&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57540&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57540&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57540&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57540&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#96528) next »