rfc:splweaktypehintingwithautoboxing

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:splweaktypehintingwithautoboxing [2010/05/31 15:31] – created tyraelrfc:splweaktypehintingwithautoboxing [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Request for Comments: How to write RFCs ======+====== Request for Comments: SPL weak type hinting ======
   * Version: 1.0   * Version: 1.0
   * Date: 2010-05-31   * Date: 2010-05-31
Line 11: Line 11:
 This rfc tries to propose an alternative solution for type hinting in PHP. This rfc tries to propose an alternative solution for type hinting in PHP.
 Basically its a weak type hinting(auto type conversion if possible without data loss), with support for scalars*, implemented as an spl interface. Basically its a weak type hinting(auto type conversion if possible without data loss), with support for scalars*, implemented as an spl interface.
 +
 +* You hint the matching Spl classname instead of the scalar name, the weak type hinting will kick-in and validate/convert the passed argument into the Spl type, but it can be used as a scalar thanks to the behaviour provided by the SplType.
  
 ===== Why weak type hinting? ===== ===== Why weak type hinting? =====
Line 24: Line 26:
 ===== Proposal ===== ===== Proposal =====
 Currently, there is no support for hinting scalar types, and only strict type hinting is allowed (there is no conversion). Currently, there is no support for hinting scalar types, and only strict type hinting is allowed (there is no conversion).
-// PHP Catchable fatal error:  Argument 1 passed to testInt::__construct() must be an instance of SplInt, integer given, ... 
-// PHP Catchable fatal error:  Argument 1 passed to testArray::__construct() must be an instance of ArrayObject, array given, ... 
  
 The idea is that we could introduce a new Spl interface, which supports the following behaviour: The idea is that we could introduce a new Spl interface, which supports the following behaviour:
Line 33: Line 33:
  
 ===== Examples ===== ===== Examples =====
-TODO+<code php> 
 +class foo implements WeakTypeHinting{ 
 +  public function bar(SplInt $baz){ 
 +    return ++$baz; 
 +  } 
 +
 + 
 +$foo = new foo; 
 +echo $foo->bar(1); 
 +// 2 
 +echo $foo->bar('2'); 
 +// 3 
 +echo $foo->bar('3a'
 +// InvalidArgumentException  
 +</code>
  
-===== Some crazy thoughts =====+===== Notes =====
 If an object implements ArrayAccess spl interface, it could be handled as an array(eg. if you hinted array, you can pass ArrayObject). If an object implements ArrayAccess spl interface, it could be handled as an array(eg. if you hinted array, you can pass ArrayObject).
rfc/splweaktypehintingwithautoboxing.1275319896.txt.gz · Last modified: 2017/09/22 13:28 (external edit)