rfc:object_cast_magic

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
rfc:object_cast_magic [2012/02/29 02:33] – Fill out more of the RFC ircmaxellrfc:object_cast_magic [2012/02/29 02:43] – [Example] ircmaxell
Line 37: Line 37:
 ==== Handler Behavior ==== ==== 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 "casted" value.  This would be called when explicitly cast (using //(int)// style syntax), when implicitly cast (passing as a parameter to an internal function expecting a specific type parameter) and when casted to a string (echo $obj;). +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 "casted" value.  This would be called when explicitly cast (using //(int)// style syntax), when implicitly cast (passing as a parameter to an internal function expecting a specific type parameter) and when casted to a string (echo $obj;). 
  
-When the get handler is called, the __castTo method is invoked with the parameter "//native//" This would indicate that the cast is to a scalar, but the type is up to the class to determine.  This is called when a primitive type is needed, but not a specific one.  So the normal math operators trigger a get call: (+, -, *, /), in addition to compound operators (++, +=, &=, |=, etc).+When the get handler is called, the //castTo// method is invoked with the parameter "//native//" This would indicate that the cast is to a scalar, but the type is up to the class to determine.  This is called when a primitive type is needed, but not a specific one.  So the normal math operators trigger a get call: (+, -, *, /), in addition to compound operators (++, +=, &=, |=, etc).
  
-When the set handler is called, the __assign method is invoked with the parameter of the new value.  The object can then react to this assignment (by setting a new internal state, by throwing an exception, etc).  If the object wants to be overwritten, it can return //FALSE//, which will cause the default assignment handler (overwriting the object) to happen.+When the set handler is called, the //assign// method is invoked with the parameter of the new value.  The object can then react to this assignment (by setting a new internal state, by throwing an exception, etc).  If the object wants to be overwritten, it can return //FALSE//, which will cause the default assignment handler (overwriting the object) to happen.
  
 ==== Example Triggers ==== ==== Example Triggers ====
Line 62: Line 62:
 ==== __toString ==== ==== __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).  Based on that, any class implementing __castTo would then ignore a __toString method if it's defined.  For backwards compatibility reasons, if __castTo is **not** defined on the class (or parent), then __toString would be executed for any string casts.+The //toString// magic method would become redundant, since it is just a limited version of the //castTo// handler (it actually implements the cast_object handler internally).  Based on that, any class implementing //castTo// would then ignore a //toString// method if it's defined.  For backwards compatibility reasons, if //castTo// is **not** defined on the class (or parent), then //toString// would be executed for any string casts.
  
-Eventually, __toString would be deprecated as duplicate functionality (but not any time soon, as it would still work fine).+Eventually, //toString// would be deprecated as duplicate functionality (but not any time soon, as it would still work fine).
  
 ==== Example ====  ==== Example ==== 
Line 91: Line 91:
           if (is_numeric($value)) {           if (is_numeric($value)) {
               $this->value = (int) $value;               $this->value = (int) $value;
 +              return;
           }           }
           return false;           return false;
Line 113: Line 114:
   * https://wiki.php.net/internals/engine/objects#cast_object   * https://wiki.php.net/internals/engine/objects#cast_object
  
-===== References ===== 
  
-  * [1] https://secure.wikimedia.org/wikipedia/en/wiki/Weak_reference 
-  * [2] https://secure.wikimedia.org/wikipedia/en/wiki/Observer_pattern 
-  * [3] http://download.oracle.com/javase/1.4.2/docs/api/java/lang/ref/WeakReference.html 
rfc/object_cast_magic.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1