rfc:returntypehint

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:returntypehint [2010/07/28 19:25]
felipe created
rfc:returntypehint [2017/09/22 13:28] (current)
Line 1: Line 1:
-====== Request for Comments: ​How to write RFCs ======+====== Request for Comments: ​Return type-hint ​======
   * Version: 1.0   * Version: 1.0
   * Date: 2010-07-28   * Date: 2010-07-28
Line 15: Line 15:
   * The new proposal has a better error message, identifying where the function/​method was called.   * The new proposal has a better error message, identifying where the function/​method was called.
   * Added generic "​numeric",​ "​scalar"​ types   * Added generic "​numeric",​ "​scalar"​ types
 +  * No needed strange syntax to solve parser conflicts
  
 ===== Examples ===== ===== Examples =====
  
 +=== Using "​scalar"​ type-hint ===
 <code php> <code php>
 <?php <?php
Line 32: Line 34:
 called in ... on line 9 and returning in ... on line 4 called in ... on line 9 and returning in ... on line 4
 */ */
 +</​code>​
 +
 +=== Using an user class type ===
 +
 +<code php>
 +<?php
 +
 +class Bar { }
 +
 +class Foo extends Bar {
 + public function Bar test($x) {
 + return $x;
 + }
 +}
 +
 +$foo = new Foo;
 +$foo->​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
 +*/
 +</​code>​
 +
 +=== Interface defining the return type-hint ===
 +
 +<code php>
 +<?php
 +
 +interface ITest {
 + function string bar();
 +}
 +
 +class Foo implements ITest {
 + public function int bar() {
 + }
 +}
 +// PHP Fatal error: ​ Declaration of Foo::bar() must be compatible with that of ITest::​bar() in ... on line 7
 +</​code>​
 +
 +=== Using a namespaced class ===
 +
 +<code php>
 +<?php
 +
 +namespace foo {
 +class foo { }
 +class bar extends foo { }
 +}
 +
 +namespace bar {
 +class bar {
 + public function \foo\foo test() {
 + return new \foo\bar;
 + }
 +}
 +
 +$bar = new bar;
 +$bar->​test();​ // ok
 +}
 </​code>​ </​code>​
  
Line 38: Line 100:
   * See below some information that will be possible to be get using Reflection.   * See below some information that will be possible to be get using Reflection.
  
-Using a class type.+=== Using a class type ===
  
 <code php> <code php>
Line 54: Line 116:
  
 $func = new ReflectionMethod('​foo::​test'​);​ $func = new ReflectionMethod('​foo::​test'​);​
-var_dump($func->​getReturnType()); // "​Test"​ +var_dump($func->​getReturnTypeHint());   ​// "​Test"​ 
-var_dump($func->​returnsObject());​ // true +var_dump($func->​returnsObject()); ​      ​// true
-var_dump($func->​returnsClass()); ​ // "​Test"​+
 </​code>​ </​code>​
  
-PHP native types+=== PHP native types ===
  
 <code php> <code php>
Line 69: Line 130:
  
 $func = new ReflectionFunction('​abc'​);​ $func = new ReflectionFunction('​abc'​);​
-var_dump($func->​getReturnType()); // scalar+var_dump($func->​getReturnTypeHint()); // scalar 
 +var_dump($func->​returnsScalar()); ​    // true
 </​code>​ </​code>​
  
 +=== Using "​self"​ as type ===
 +
 +<code php>
 +<?php
 +
 +class foo {
 + function self bar() {
 + return new foo;
 + }
 +}
 +
 +$func = new ReflectionMethod('​foo::​bar'​);​
 +var_dump($func->​getReturnTypeHint());​ // foo
 +var_dump($func->​returnsObject()); ​    // true
 +</​code>​
  
 ===== Patch ===== ===== Patch =====
  
-Comming soon+  * Engine + Reflection: http://​felipe.ath.cx/​diff/​returntypehint.diff 
 +  * Tests: http://​felipe.ath.cx/​diff/​tests/​return_typehint_tests.zip
rfc/returntypehint.1280345125.txt.gz · Last modified: 2017/09/22 13:28 (external edit)