note 38146 added to function.unserialize
| From: | TerryJohnson at rn2 dot php dot net | Date: | Tue, 09 Dec 2003 22:27:05 +0000 |
| Subject: | note 38146 added to function.unserialize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-61713@lists.php.net to get a copy of this message | ||
It is possible to make a neat autoloader for class definitions using this, but there are some
gotchas for the unwary programmer:
1) If you are setting the unserialize_callback_func directive in the ini or .htaccess file, use
auto_prepend_file to load the definition of your callback function - otherwise objects that stay in
the session may trigger errors on pages where you didn't expect the object to be used.
2) It helps if you define all your classes in files with lowercase file names from the beginning.
The callback function is always call with the class name in lower case, and it is a lot quicker to
use that directly than make a lookup table:
function callback_func($classname) {
@include_once("${classname}.class.php");
}
3) It does not appear to be possible to use a static member fuction of a class (for example, a your
object persistence layer) as the unserialize callback function, so this will cause confusion:
php_value auto_prepend_file "Persist.php"
php_value unserialize_callback_func Persist::factory
The next best solution is to make it a function in the global scope and have your object factory
call it as required:
Class Persist
{
function &factory($type) {
callback_func(strtolower($type));
$classname = "${type}";
if (!class_exists($classname)) {
return PEAR::raiseError('Class Not
Found',PERSIST_ERROR_CLASS_NOT_FOUND,PEAR_ERROR_RETURN);
}
@$obj =& new $classname;
return $obj;
}
...
}
----
Manual Page -- http://www.php.net/manual/en/function.unserialize.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38146
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38146&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38146&report=yes
Search -- http://master.php.net/manage/user-notes.php