====== Request for Comments: Return type-hint ====== * Version: 1.0 * Date: 2010-07-28 * Author: Felipe Pena * Status: Under Discussion * First Published at: http://wiki.php.net/rfc/typehint ===== 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 * No needed strange syntax to solve parser conflicts ===== Examples ===== === Using "scalar" type-hint === === Using an user class type === test($foo); // ok $foo->test(new stdClass); // fail /* PHP Catchable fatal error: The returned value should be instance of Bar, called in ... on line 13 and returning in ... on line 7 */ === Interface defining the return type-hint === === Using a namespaced class === test(); // ok } ===== Reflection ===== * See below some information that will be possible to be get using Reflection. === Using a class type === getReturnTypeHint()); // "Test" var_dump($func->returnsObject()); // true === PHP native types === getReturnTypeHint()); // scalar var_dump($func->returnsScalar()); // true === Using "self" as type === getReturnTypeHint()); // foo var_dump($func->returnsObject()); // true ===== Patch ===== * Engine + Reflection: http://felipe.ath.cx/diff/returntypehint.diff * Tests: http://felipe.ath.cx/diff/tests/return_typehint_tests.zip