rfc:object_cast_magic
Differences
This shows you the differences between two versions of the page.
Next revisionBoth sides next revision | |||
rfc:object_cast_magic [2012/02/29 02:16] – created ircmaxell | rfc:object_cast_magic [2012/02/29 02:33] – Fill out more of the RFC ircmaxell | ||
---|---|---|---|
Line 19: | Line 19: | ||
* Creating new semi-primitive types with functional limitations applied. | * Creating new semi-primitive types with functional limitations applied. | ||
* Creating constant (const) variables that can be written to once (on creation), but not again. | * Creating constant (const) variables that can be written to once (on creation), but not again. | ||
+ | |||
+ | ==== Boolean Logic ==== | ||
+ | |||
+ | Right now, when you use an object in a boolean context, it always evaluates to // | ||
==== Providing Backwards Compatibility ==== | ==== Providing Backwards Compatibility ==== | ||
Line 31: | Line 35: | ||
bool __assign(mixed $newValue) | bool __assign(mixed $newValue) | ||
- | When the cast_object handler is called, the __castTo method is invoked with a string representation of the type to be casted to. The return value is then used as the " | + | ==== Handler Behavior ==== |
+ | |||
+ | When the cast_object handler is called, the __castTo method is invoked with a string representation of the type to be casted to. The return value is then used as the " | ||
+ | |||
+ | When the get handler is called, the __castTo method is invoked with the parameter "// | ||
+ | |||
+ | When the set handler is called, the __assign method is invoked with the parameter of the new value. | ||
+ | |||
+ | ==== Example Triggers ==== | ||
+ | |||
+ | <?php | ||
+ | |||
+ | | ||
+ | $obj = $obj + 1; // same as above | ||
+ | $obj |= 5; // triggers __castTo(" | ||
+ | |||
+ | $obj = 5; // triggers __assign(5) | ||
+ | $obj = array(1, 2, 3); // triggers __assign(array(1, | ||
+ | $obj = new StdClass; // triggers __assign(new StdClass) | ||
+ | |||
+ | if ($obj) // triggers __castTo(" | ||
+ | |||
A proof of concept patch is available here: [[https:// | A proof of concept patch is available here: [[https:// | ||
+ | |||
+ | ==== __toString ==== | ||
+ | |||
+ | The __toString method would become redundant, since it is just a limited version of the __castTo handler (it actually implements the cast_object handler internally). | ||
+ | |||
+ | Eventually, __toString would be deprecated as duplicate functionality (but not any time soon, as it would still work fine). | ||
==== Example ==== | ==== Example ==== |
rfc/object_cast_magic.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1