cast_object() handler
| From: | Sterling Hughes | Date: | Sat, 24 May 2003 22:49:15 +0000 |
| Subject: | cast_object() handler | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-1972@lists.php.net to get a copy of this message | ||
Hi,
Attached is a patch which allows one to intercept calls to object
casting within the engine. This means that whenever an object is
converted to a base type (except array, which has different semantics),
a special object handler is called instead of the default conversion
function.
I've come across a need for this when implementing the simplexml
extension, where i want an object to masquerade as either a string or a
node. Take the following:
slide.xml
<?xml version="1.0"?>
<slide>
<list>
<bullet effect="slide">Why SimpleXML is cool</bullet>
</list>
</slide>
With this new callback, the following syntax is perfectly valid:
<?php
$list = simplexml_load_file('slide.xml')->list;
echo $list->bullet; // "Why SimpleXML is cool"
echo $list->bullet->effect; // "slide"
?>
Attached is a patch that makes this so. It only affects objects, and it
is only a internal syntax (not exported to userspace). Every other
place in the code that doesn't exploit this callback (sets it to NULL)
will continue to operate as normal.
-Sterling
--
"Nothing is particularly hard if you divide it into small jobs."
- Henry Ford
Attachment: [text/x-patch] cast_object.diff
Attachment: [text/x-patch] cast_object.diff