rfc:typehint

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:typehint [2008/04/08 00:20] – created feliperfc:typehint [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 3: Line 3:
   * Date: 2008-04-07   * Date: 2008-04-07
   * Author: Felipe Pena <felipensp@gmail.com>   * Author: Felipe Pena <felipensp@gmail.com>
-  * Status: Under Discussion+  * Status: [[http://news.php.net/php.internals/37049|Under Discussion]]
  
-===== Parameter type hint - Examples ===== + 
-=== integer / int ===+====== Details of implementation ====== 
 +**Parameter type hint:** 
 +  The behavior when the default parameter value is ''NULL'' was kept. 
 +**Both:** 
 +  - Binary and unicode string are specified by ''string''
 + 
 +===== Return value type hint ===== 
 + 
 +==== BC Break ==== 
 +No BC break using the "(type)" syntax. (Inspirated by Objective-C) 
 + 
 + 
 +==== Examples ==== 
 + 
 +=== Using inside namespaces ===
 <code php> <code php>
-function test(integer $value) {+namespace foo; 
 + 
 +class test { } 
 + 
 +class bar { 
 + static public function (foo::test) testing($instance) { 
 + return $instance; 
 + }
 } }
  
-test(1); +bar::testing(new test); 
-test("1337"); +bar::testing(new stdClass); // Catchable fatal error: The returned value should be instance of foo::test 
-test(-1); +</code>
-test("1."); // Catchable fatal error: Argument 1 passed to test() must be an integer, string given+
  
 +=== Using inheritance ===
 +<code php>
 +interface ITest { }
 +class bar implements ITest { }
 +class foo extends bar { }
 +
 +function (Itest) testing($instance) {
 + return $instance;
 +}
 +
 +testing(new bar);
 +testing(new foo);
 +testing(new stdClass); // Catchable fatal error: The returned value must implement interface Itest
 +</code>
 +
 +=== Interfaces ===
 +
 +<code php>
 +interface ITest {
 + public function (int) foo();
 +}
 +
 +class foo implements ITest {
 + public function (int) foo() {
 + return 'a';
 + }
 +}
 +
 +$test = new foo;
 +$test->foo(); // Catchable fatal error: The returned value must be of the type integer
 +</code>
 +
 +<code php>
 +interface ITest {
 + public function (int) foo();
 +}
 +
 +class foo implements ITest {
 + public function foo() {
 + return 1;
 + }
 +}
 +
 +$test = new foo;
 +$test->foo(); // Fatal error: Declaration of foo::foo() must be compatible with that of ITest::foo()
 +</code>
 +
 +=== Trying use with magic methods ===
 +<code php>
 +class test {
 + // Fatal error: Return type hint can't be used with magic methods
 + public function (int) __toString() {
 + }
 +}
 +</code>
 +
 +=== PHP types ===
 +<code php>
 +function (int) test($value) {
 + return $value;
 +}
 +
 +test('1337'); // Catchable fatal error: The returned value must be of the type integer
 +</code>
 +
 +
 +===== Parameter type hint =====
 +
 +==== BC Break ====
 +
 +== New tokens (new keywords) ==
 +  - T_INTEGER (Keywords: int, integer)
 +  - T_BOOLEAN (Keywords: bool, boolean)
 +  - T_DOUBLE (Keywords: real, float, double)
 +  - T_OBJECT (Keyword: object)
 +  - T_RESOURCE (Keyword: resource)
 +  - T_STR (Keyword: string)
 +
 +==== Other changes ====
 +  - Removed ZEND_ARG_ARRAY_INFO()
 +  - Added ZEND_ARG_PHP_TYPE_INFO()
 +<code diff u>
 +-#define ZEND_ARG_ARRAY_INFO(pass_by_ref, name, allow_null) { {#name}, sizeof(#name)-1, {NULL}, 0, 1, allow_null, pass_by_ref, 0, 0 },
 ++#define ZEND_ARG_PHP_TYPE_INFO(pass_by_ref, name, php_type, allow_null) { {#name}, sizeof(#name)-1, {NULL}, 0, php_type, allow_null, pass_by_ref, 0, 0 },
 +</code>
 +  - **Reflection**: ReflectionParameter class:
 +    - Added:
 +      - isInt()
 +      - isDouble()
 +      - isBool()
 +      - isString()
 +      - isObject()
 +      - isResource()
 +
 +
 +
 +==== Examples ====
 +
 +=== With default parameter value ===
 +<code php>
 function test(integer $value = '1') { function test(integer $value = '1') {
 } }
 // Fatal error: Default value for parameters with integer type hint can only be the exact type or NULL // Fatal error: Default value for parameters with integer type hint can only be the exact type or NULL
 +</code>
 +
 +=== integer / int ===
 +<code php>
 +function test(integer $value) {
 +}
 +
 +test(1);
 +test(-1);
 +test("1."); 
 +// Catchable fatal error: Argument 1 passed to test() must be of the type integer, string given ...
 </code> </code>
  
Line 28: Line 159:
 test(1.1); test(1.1);
 test(.1); test(.1);
-test("1."); +test("1."); // Catchable fatal error: Argument 1 passed to test() must be of the type double, string given
-test("1337"); // Catchable fatal error: Argument 1 passed to test() must be an double, string given +
- +
-function test(double $value = '1') { +
-+
-// Fatal error: Default value for parameters with double type hint can only be the exact type or NULL+
 </code> </code>
  
Line 42: Line 168:
  
 test(false); test(false);
-test(0); +test(0); // Catchable fatal error: Argument 1 passed to test() must be of the type boolean, null given
-test(1); +
-test('0'); +
-test('1'); +
-test(''); +
-test(null); // Catchable fatal error: Argument 1 passed to test() must be an boolean, null given+
 </code> </code>
  
Line 56: Line 177:
  
 test(fopen(__FILE__, 'r')); test(fopen(__FILE__, 'r'));
-test(NULL); // Catchable fatal error: Argument 1 passed to test() must be an resource, null given+test(NULL); // Catchable fatal error: Argument 1 passed to test() must be of the type resource, null given
 </code> </code>
  
Line 65: Line 186:
  
 test(new stdclass); test(new stdclass);
-test(NULL); // Catchable fatal error: Argument 1 passed to test() must be an object, null given +test(NULL); // Catchable fatal error: Argument 1 passed to test() must be of the type object, null given
- +
-function test(object $value = 1) { +
-+
-// Fatal error: Default value for parameters with object type hint can only be the exact type or NULL+
 </code> </code>
  
  
-===== Proposal and Patch ===== 
  
-Parameter type hint: +===== Patches & tests =====
  
 +  - Return value type hint: http://felipe.ath.cx/diff/return_type_hint.diff
 +  - Parameter type hint: http://felipe.ath.cx/diff/param_type_hint.diff
 +  - Tests: http://felipe.ath.cx/diff/tests/
  
rfc/typehint.1207614003.txt.gz · Last modified: 2017/09/22 13:28 (external edit)