note 77538 added to function.unserialize
| From: | cory at lavacube dot com | Date: | Mon, 03 Sep 2007 01:45:19 +0000 |
| Subject: | note 77538 added to function.unserialize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130111@lists.php.net to get a copy of this message | ||
Problem: I needed a way to call an object that I did not know the name of. This forced me to use a
variable for the class name.
Solution: unserialize();
Unserialize supports opening objects/classes from a serialized string. In order to do so I had to do
something to this effect:
<?php
// this is the variable part
$class_name = 'Some_Obscure_Class';
// here is where we take the name and dynamically create a serialize-compliant string.
$quickload = 'O:' . strlen($class_name) . ':"' .$class_name.
'":0:{}';
// and here we inject the string into unserialize, this creating an object! :-)
$object = unserialize($quickload);
?>
From here I used the __wakeup() magic function to initialize/construct the class as needed. :-)
----
Server IP: 216.235.15.211
Probable Submitter: 142.165.95.174
----
Manual Page -- http://www.php.net/manual/en/function.unserialize.php
Edit -- https://master.php.net/note/edit/77538
Del: integrated -- https://master.php.net/note/delete/77538/integrated
Del: useless -- https://master.php.net/note/delete/77538/useless
Del: bad code -- https://master.php.net/note/delete/77538/bad+code
Del: spam -- https://master.php.net/note/delete/77538/spam
Del: non-english -- https://master.php.net/note/delete/77538/non-english
Del: in docs -- https://master.php.net/note/delete/77538/in+docs
Del: other reasons-- https://master.php.net/note/delete/77538
Reject -- https://master.php.net/note/reject/77538
Search -- https://master.php.net/manage/user-notes.php