rfc:typehint

Request for Comments: Return value and parameter type hint

Details of implementation

Parameter type hint:

  1. The behavior when the default parameter value is NULL was kept.

Both:

  1. 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

namespace foo;
 
class test { }
 
class bar {
	static public function (foo::test) testing($instance) {
		return $instance;
	}
}
 
bar::testing(new test);
bar::testing(new stdClass); // Catchable fatal error: The returned value should be instance of foo::test

Using inheritance

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

Interfaces

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
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()

Trying use with magic methods

class test {
	// Fatal error: Return type hint can't be used with magic methods
	public function (int) __toString() {
	}
}

PHP types

function (int) test($value) {
	return $value;
}
 
test('1337'); // Catchable fatal error: The returned value must be of the type integer

Parameter type hint

BC Break

New tokens (new keywords)
  1. T_INTEGER (Keywords: int, integer)
  2. T_BOOLEAN (Keywords: bool, boolean)
  3. T_DOUBLE (Keywords: real, float, double)
  4. T_OBJECT (Keyword: object)
  5. T_RESOURCE (Keyword: resource)
  6. T_STR (Keyword: string)

Other changes

  1. Removed ZEND_ARG_ARRAY_INFO()
  2. Added ZEND_ARG_PHP_TYPE_INFO()
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 },
  1. Reflection: ReflectionParameter class:
    1. Added:
      1. isInt()
      2. isDouble()
      3. isBool()
      4. isString()
      5. isObject()
      6. isResource()

Examples

With default parameter value

function test(integer $value = '1') {
}
// Fatal error: Default value for parameters with integer type hint can only be the exact type or NULL

integer / int

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 ...

double / float / real

function test(double $value) {
}
 
test(1.1);
test(.1);
test("1."); // Catchable fatal error: Argument 1 passed to test() must be of the type double, string given

bool / boolean

function test(bool $value = true) {
}
 
test(false);
test(0); // Catchable fatal error: Argument 1 passed to test() must be of the type boolean, null given

resource

function test(resource $value) {
}
 
test(fopen(__FILE__, 'r'));
test(NULL); // Catchable fatal error: Argument 1 passed to test() must be of the type resource, null given

object

function test(object $value) {
}
 
test(new stdclass);
test(NULL); // Catchable fatal error: Argument 1 passed to test() must be of the type object, null given

Patches & tests

rfc/typehint.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1