NYCPHP Meetup

NYPHP.org

[nycphp-talk] unserialize_callback_func

Hans Zaunere zaunere at yahoo.com
Mon Oct 28 11:53:48 EST 2002


--- Adam Fields <fields at surgam.net> wrote:
> 
> I'm trying to use unserialize_callback_func to set up an autoloader
> function to be called if an object is unserialized and the class
> hasn't been defined yet.
> 
> However, it doesn't seem to work - the callback function isn't
> getting called.
> 
> I tried ini_set and setting the value explicitly in the php.ini, but
> it doesn't call the function.
> 
> Drawing on the example from the docs:
> 
> -------------------
> function __unser($class)
> {
>   die ("Callback");
> }
> ini_set('unserialize_callback_func','__unser');
> $serialized_object='O:1:"a":1:{s:5:"value";s:3:"100";}';
> print_r (unserialize($serialized_object));
> -------------------
> 
> should print "Callback", but instead, it prints (as expected without
> the callback) "Array ( ) __PHP_Incomplete_Class Object (
> [__PHP_Incomplete_Class_Name] => a [value] => 100 ) ".
> 
> Has anyone used this successfully?

Yeah, I'm using this right now.  The example looks like it should work,
however consider a couple things:
  -- Drop the underscores from the function's name (use a plain-jane
function name initially for testing).
  -- Instead of die() in the callback, use trigger_error(), as dieing
might not act as expected when within a callback.

> Also, assuming there's a way to do this, does anyone know if this
> callback can be a class method for a singleton class instead of a
> regular function?

Not sure, but I can't think of why not.



=====
Hans Zaunere
New York PHP
http://nyphp.org
hans at nyphp.org

__________________________________________________
Do you Yahoo!?
Y! Web Hosting - Let the expert host your web site
http://webhosting.yahoo.com/



More information about the talk mailing list