rfc:typehint
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:typehint [2008/04/08 03:41] – New informations felipe | rfc: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 < | * Author: Felipe Pena < | ||
- | * Status: Under Discussion | + | * Status: |
- | ===== BC Break ===== | ||
- | == New tokens (new keywords) | + | ====== Details of implementation ====== |
- | - T_INTEGER (Keywords: int, integer) | + | **Parameter type hint:** |
- | - T_BOOLEAN (Keywords: bool, boolean) | + | - The behavior when the default parameter value is '' |
- | - T_DOUBLE (Keywords: real, float, double) | + | **Both:** |
- | - T_OBJECT (Keyword: object) | + | - Binary and unicode string are specified by '' |
- | - T_RESOURCE (Keyword: resource) | + | |
- | - T_STR (Keyword: | + | |
===== Return value type hint ===== | ===== Return value type hint ===== | ||
+ | |||
+ | ==== BC Break ==== | ||
+ | No BC break using the " | ||
+ | |||
+ | |||
+ | ==== Examples ==== | ||
=== Using inside namespaces === | === Using inside namespaces === | ||
Line 46: | Line 49: | ||
testing(new foo); | testing(new foo); | ||
testing(new stdClass); // Catchable fatal error: The returned value must implement interface Itest | testing(new stdClass); // Catchable fatal error: The returned value must implement interface Itest | ||
+ | </ | ||
+ | |||
+ | === Interfaces === | ||
+ | |||
+ | <code php> | ||
+ | interface ITest { | ||
+ | public function (int) foo(); | ||
+ | } | ||
+ | |||
+ | class foo implements ITest { | ||
+ | public function (int) foo() { | ||
+ | return ' | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $test = new foo; | ||
+ | $test-> | ||
+ | </ | ||
+ | |||
+ | <code php> | ||
+ | interface ITest { | ||
+ | public function (int) foo(); | ||
+ | } | ||
+ | |||
+ | class foo implements ITest { | ||
+ | public function foo() { | ||
+ | return 1; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $test = new foo; | ||
+ | $test-> | ||
+ | </ | ||
+ | |||
+ | === 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() { | ||
+ | } | ||
+ | } | ||
</ | </ | ||
Line 54: | Line 98: | ||
} | } | ||
- | test(' | + | test(' |
- | test(-1); | + | |
- | test(1); | + | |
- | test(1.); // Catchable fatal error: The returned value needs be integer | + | |
</ | </ | ||
- | ===== Parameter type hint - Examples ===== | + | |
+ | ===== Parameter type hint ===== | ||
+ | |||
+ | ==== BC Break ==== | ||
+ | |||
+ | == New tokens (new keywords) == | ||
+ | | ||
+ | - 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, | ||
+ | +#define ZEND_ARG_PHP_TYPE_INFO(pass_by_ref, | ||
+ | </ | ||
+ | - **Reflection**: | ||
+ | - Added: | ||
+ | - isInt() | ||
+ | - isDouble() | ||
+ | - isBool() | ||
+ | - isString() | ||
+ | - isObject() | ||
+ | - isResource() | ||
+ | |||
+ | |||
+ | |||
+ | ==== Examples ==== | ||
+ | |||
+ | === With default parameter value === | ||
+ | <code php> | ||
+ | function test(integer $value = ' | ||
+ | } | ||
+ | // Fatal error: Default value for parameters with integer type hint can only be the exact type or NULL | ||
+ | </ | ||
=== integer / int === | === integer / int === | ||
Line 68: | Line 147: | ||
test(1); | test(1); | ||
- | test(" | ||
test(-1); | test(-1); | ||
- | test(" | + | test(" |
- | + | // Catchable fatal error: Argument 1 passed to test() must be of the type integer, string given ... | |
- | function test(integer $value = ' | + | |
- | } | + | |
- | // Fatal error: Default value for parameters with integer type hint can only be the exact type or NULL | + | |
</ | </ | ||
Line 84: | Line 159: | ||
test(1.1); | test(1.1); | ||
test(.1); | test(.1); | ||
- | test(" | + | test(" |
- | test(" | + | |
- | + | ||
- | function test(double $value = ' | + | |
- | } | + | |
- | // Fatal error: Default value for parameters with double type hint can only be the exact type or NULL | + | |
</ | </ | ||
Line 98: | 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(' | + | |
- | test(' | + | |
- | test('' | + | |
- | test(null); // Catchable fatal error: Argument 1 passed to test() must be an boolean, null given | + | |
</ | </ | ||
Line 112: | Line 177: | ||
test(fopen(__FILE__, | test(fopen(__FILE__, | ||
- | 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 |
</ | </ | ||
Line 121: | 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 | ||
- | </ | ||
- | ===== Patches ===== | + | ===== Patches |
- Return value type hint: http:// | - Return value type hint: http:// | ||
- Parameter type hint: http:// | - Parameter type hint: http:// | ||
+ | - Tests: http:// | ||
rfc/typehint.1207626081.txt.gz · Last modified: 2017/09/22 13:28 (external edit)