note 50791 added to language.types.type-juggling
| From: | toma at smartsemantics dot com | Date: | Thu, 10 Mar 2005 02:24:19 +0000 |
| Subject: | note 50791 added to language.types.type-juggling | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-86186@lists.php.net to get a copy of this message | ||
In my much of my coding I have found it necessary to type-cast between objects of different class
types.
More specifically, I often want to take information from a database, convert it into the class it
was before it was inserted, then have the ability to call its class functions as well.
The following code is much shorter than some of the previous examples and seems to suit my purposes.
It also makes use of some regular expression matching rather than string position, replacing, etc.
It takes an object ($obj) of any type and casts it to an new type ($class_type). Note that the new
class type must exist:
function ClassTypeCast(&$obj,$class_type){
if(class_exists($class_type,true)){
$obj = unserialize(preg_replace"/^O:[0-9]+:\"[^\"]+\":/i",
"O:".strlen($class_type).":\"".$class_type."\":",
serialize($obj)));
}
}
----
Manual Page -- http://www.php.net/manual/en/language.types.type-juggling.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50791
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50791&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50791&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50791&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50791&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50791&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50791&report=yes
Search -- http://master.php.net/manage/user-notes.php