rfc:returntypehint

This is an old revision of the document!


Request for Comments: How to write RFCs

Introduction

The purpose of the RFC is to discuss about the return type hint utility on PHP.

Differences from the old proposal

  • The old proposal (return and param type-hint) was introducing some keywords (the type names), with this new proposal there is no such BC.
  • The new proposal has a better error message, identifying where the function/method was called.
  • Added generic “numeric”, “scalar” types

Examples

<?php
 
function scalar abc($x = NULL) {
	return $x;
}
 
var_dump(abc(1));  // int(1)
var_dump(abc(1.)); // float(1)
var_dump(abc());
/*
PHP Catchable fatal error:  The returned value must be of the type scalar,
called in ... on line 9 and returning in ... on line 4
*/

Reflection

  • See below some information that will be possible to be get using Reflection.

Using a class type

<?php
 
interface Test {
 
}
 
class foo implements Test {	
	function Test test() {
		return new foo;
	}
}
 
$func = new ReflectionMethod('foo::test');
var_dump($func->getReturnType()); // "Test"
var_dump($func->returnsObject()); // true
var_dump($func->returnsClass());  // "Test"

PHP native types

<?php
 
function scalar abc($x = NULL) {
	return $x;
}
 
$func = new ReflectionFunction('abc');
var_dump($func->getReturnType()); // scalar

Patch

Comming soon

rfc/returntypehint.1280345190.txt.gz · Last modified: 2017/09/22 13:28 (external edit)