note 38146 deleted from function.unserialize by aharvey

From: Date: Tue, 06 Nov 2012 01:17:59 +0000
Subject: note 38146 deleted from function.unserialize by aharvey
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-191685@lists.php.net to get a copy of this message
Note Submitter: Terry Johnson ---- 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; } ... }

« previous php.notes (#191685) next »