rfc:object_cast_to_types

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
rfc:object_cast_to_types [2013/09/03 12:28]
ircmaxell Withdrawn
rfc:object_cast_to_types [2017/09/22 13:28] (current)
Line 1: Line 1:
 ===== Request for Comments: Scalar Type Casting Magic Methods ===== ===== Request for Comments: Scalar Type Casting Magic Methods =====
-  * Version: 0.1+  * Version: 0.2
   * Date: 2012-03-03   * Date: 2012-03-03
   * Author: Anthony Ferrara <​ircmaxell@php.net>​   * Author: Anthony Ferrara <​ircmaxell@php.net>​
-  * Status: ​Withdrawn+  ​* Maintainer: Matiss Treinis <​mrtreinis@gmail.com>​ 
 +  ​* Status: ​In Draft (Inactive)
   * Patch: https://​gist.github.com/​1966809   * Patch: https://​gist.github.com/​1966809
  
 ===== Introduction ===== ===== Introduction =====
  
-Currently, the Zend Engine exposes several methods to PECL extensions and internal classes to control how internal objects behave when casting to primitives. ​ The two methods used in this RFC are specifically ''​cast_object()''​ and ''​get()''​ .  This proposal is to expose those two methods as a series of new magic methods to allow user classes to hook into this functionality more completely.+Currently, the Zend Engine exposes several methods to PECL extensions and internal classes to control how internal objects behave when casting to primitives. ​ The two methods used in this RFC are specifically ''​cast_object()''​ and ''​get()''​ .  This proposal is to expose those two methods as a series of new magic methods to allow user classes to hook into this functionality more completely.
  
 ===== Use Cases ===== ===== Use Cases =====
Line 18: Line 19:
    * Creating strict-typing functionality with entirely user-land code (having a variable that until unset cannot be assigned to with anything except an integer for example)    * Creating strict-typing functionality with entirely user-land code (having a variable that until unset cannot be assigned to with anything except an integer for example)
    * Having object code be compatible with existing scalar-typed code.  An example would be SplFixedArray would be enabled to be passed to any function (including internal functions) that expect arrays which are not passed by reference.    * Having object code be compatible with existing scalar-typed code.  An example would be SplFixedArray would be enabled to be passed to any function (including internal functions) that expect arrays which are not passed by reference.
 +   * Allows for objects to carry a Boolean state (to be truthy and falsy)
  
 ===== Implementation ===== ===== Implementation =====
Line 29: Line 31:
   * ''​_ _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''​).
 +  * ''​_ _toBool()''​ - Called when casting an object explicitly to a boolean (or passing it to an internal function which expects a boolean)
  
 ==== 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()''​''​toString()''​ and ''​toBool()''​.
  
 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).
Line 45: Line 48:
    ​(array) $obj; // triggers __toArray()    ​(array) $obj; // triggers __toArray()
    ​array_keys($obj);​ // triggers __toArray()    ​array_keys($obj);​ // triggers __toArray()
 +   ​(bool) $obj; // triggers __toBool()
  
 ==== Backwards Compatibility Breaks ==== ==== Backwards Compatibility Breaks ====
Line 57: Line 61:
  
 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.
 +
 +===== Todo =====
 +
 +  * Patch could use an update.
 +  * How could and will this interact with comparison and control structures like, ''​if($obj)''​ ?
  
 ===== Further reading ===== ===== Further reading =====
rfc/object_cast_to_types.1378211323.txt.gz · Last modified: 2017/09/22 13:28 (external edit)