rfc:object_cast_to_types

Differences

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

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
rfc:object_cast_to_types [2012/03/04 01:21] – created ircmaxellrfc:object_cast_to_types [2013/09/03 12:28] – Withdrawn ircmaxell
Line 3: Line 3:
   * Date: 2012-03-03   * Date: 2012-03-03
   * Author: Anthony Ferrara <ircmaxell@php.net>   * Author: Anthony Ferrara <ircmaxell@php.net>
-  * Status: In Draft+  * Status: Withdrawn
   * Patch: https://gist.github.com/1966809   * Patch: https://gist.github.com/1966809
  
Line 25: Line 25:
 This RFC and patch introduces 4 new magic methods: This RFC and patch introduces 4 new magic methods:
  
- * ''__toInt()'' - Called when casting an object explicitly to an integer (or passing it to an internal function which expects an integer) +  * ''_ _toInt()'' - Called when casting an object explicitly to an integer (or passing it to an internal function which expects an integer) 
- * ''__toFloat()'' - Called when casting an object explicitly to a float (or passing it to an internal function which expects a float) +  * ''_ _toFloat()'' - Called when casting an object explicitly to a float (or passing it to an internal function which expects a float) 
- * ''__toArray()'' - Called when casting an object explicitly to an array (or passing it to an internal function which expects an array) +  * ''_ _toArray()'' - Called when casting an object explicitly to an array (or passing it to an internal function which expects an array) 
- * ''__toScalar()'' - Called when using an object in an implicit scalar scope without type information (for example: ''$obj + 1'').+  * ''_ _toScalar()'' - Called when using an object in an implicit scalar scope without type information (for example: ''$obj + 1'').
  
 ==== Handler Behavior ==== ==== Handler Behavior ====
  
-When the cast_object handler is called, the type parameter is switched against to determine which magic method to call.  The four possibilities include ''__toInt()'', ''__toFloat()'', ''__toArray()'' and ''__toString()''.+When the cast_object handler is called, the type parameter is switched against to determine which magic method to call.  The four possibilities include ''toInt()'', ''toFloat()'', ''toArray()'' and ''toString()''.
  
-When the get handler is called, the ''__toScalar()'' method is invoked.  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 ''toScalar()'' method is invoked.  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).
  
 ==== Example Triggers ==== ==== Example Triggers ====
Line 56: Line 56:
 The normal casting logic does not make a distinction between how casts happen with references.  So this patch does not attempt to make a distinction there.  However, when an internal function accepts a typed parameter by reference, if the magic cast method is defined on the passed in object, an error is raised as the cast cannot be performed because of the reference. The normal casting logic does not make a distinction between how casts happen with references.  So this patch does not attempt to make a distinction there.  However, when an internal function accepts a typed parameter by reference, if the magic cast method is defined on the passed in object, an error is raised as the cast cannot be performed because of the reference.
  
-This means that an object implementing __toArray cannot be passed directly to ''sort()'', as that would cause the object to be destroyed.  So, to prevent data loss, references are prevented from automated casting.+This means that an object implementing ''toArray'' cannot be passed directly to ''sort()'', as that would cause the object to be destroyed.  So, to prevent data loss, references are prevented from automated casting.
  
 ===== Further reading ===== ===== Further reading =====
Line 65: Line 65:
   * https://wiki.php.net/internals/engine/objects#set   * https://wiki.php.net/internals/engine/objects#set
   * https://wiki.php.net/internals/engine/objects#cast_object   * https://wiki.php.net/internals/engine/objects#cast_object
- 
rfc/object_cast_to_types.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1