cast_object() handler

php.internals

Sterling Hughes

23 years ago
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

Ants Aasma

23 years ago
"Sterling Hughes" <sterling@bumblebury.com> wrote in message news:1053816554.355.13.camel@hasele...
> 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.
...
> 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.
Why not export this to userspace? At least for ZE2. This would allow for some pretty nice codeconstructs with probably negligible performance hit, and close to zero BC problems. For example if __toString() is defined in a class then that function is called if an instance of that class is used in string context. Actually thought of this independently a couple of hours ago, sorry if this has been discussed. ------------- Ants Aasma