note 57540 deleted from language.oop5.autoload by felipe
| From: | felipe@php.net | Date: | Mon, 24 Mar 2008 22:33:13 +0000 |
| Subject: | note 57540 deleted from language.oop5.autoload by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-137494@lists.php.net to get a copy of this message | ||
Note Submitter: shurikk at gmail dot com
----
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