note 77538 deleted from function.unserialize by aharvey
| From: | aharvey@php.net | Date: | Tue, 06 Nov 2012 01:32:05 +0000 |
| Subject: | note 77538 deleted from function.unserialize by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-191712@lists.php.net to get a copy of this message | ||
Note Submitter: cory at lavacube dot com
----
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. :-)