note 52685 added to keyword.paamayim-nekudotayim
| From: | (firstname@lastname.com) | Date: | Mon, 09 May 2005 21:33:42 +0000 |
| Subject: | note 52685 added to keyword.paamayim-nekudotayim | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89178@lists.php.net to get a copy of this message | ||
In reply to tim dot ward at stivesdirect dot com:
I had a need to to do a very similar thing. I wrote a class that will be used to create an object
which can serialize itself into a cookie and be used to recover itself from the cookie later. To do
the recovery you must use the Scope Resolution Operator (::) to call a function as a class function
(vs. and object function). The recovery is very simple to initiate because there is only one line
of code to call (e.g. $the_object = the_class::the_function(); ) But the function you call must do
a ton of stuff, and call a few other functions within the class. This is where the problem lies.
I would like to thank <brooke at jump dot net>, <pollita at php dot net>, and
<MagicalTux at FF.ST> (from http://php.net/manual/en/function.get-class.php)
for leading me to the solution.
<?php
class foo
{
function get_object_from_cookie(...)
{
...
return unserialize($decrypted_cookie_data);
}
function recover_from_cookie()
{
...prepare to recover...
// The next line is the tricky part.
$o = call_user_func(array(__CLASS__, 'get_object_from_cookie'), $encryption_secret,
$cookie_name);
$temp = $o->get_stuff();
$o->other_stuff = $o->do_calculate($temp);
$o->do_complicated_things();
return $o;
}
}
$bar = foo::recover_from_cookie();
...
?>
I hope that saves someone an hour. It sure would have saved me one.
----
Manual Page -- http://www.php.net/manual/en/keyword.paamayim-nekudotayim.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52685
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52685&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52685&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52685&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52685&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52685&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52685&report=yes
Search -- http://master.php.net/manage/user-notes.php