rfc:instance-method-call

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:instance-method-call [2010/11/26 19:04] – update feliperfc:instance-method-call [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 3: Line 3:
   * Date: 2010-11-26   * Date: 2010-11-26
   * Author: Felipe Pena <felipe@php.net>   * Author: Felipe Pena <felipe@php.net>
-  * Status: Under Discussion+  * Status: Implemented (Syntax 2)
  
 ===== Introduction ===== ===== Introduction =====
  
-The purpose of RFC is to presents the support of instancing and method call/property access on same command.+The purpose of RFC is to presents the support for instantiating a class and calling its methods and accessing its properties on same command. 
 +We could use one of two syntaxes below:
  
-==== Examples ====+===== Syntax 1 (without brackets) ===== 
 + 
 +  * %%new foo->bar()%% should be read as %%(new foo)->bar()%% 
 +  * %%new $foo()->bar%% should be read as %%(new $foo())->bar%% 
 +  * %%new $bar->y()->x%% should be read as %%(new ($bar->y)())->x%% 
 + 
 +===== Syntax 2 (with brackets) ===== 
 + 
 +  * %%(new foo())->bar()%% 
 +  * %%(new $foo())->bar%% 
 +  * %%(new $bar->y)->x%% 
 +  * %%(new foo)[0]%% 
 + 
 +===== Examples ====
 + 
 +=== Using brackets === 
 + 
 +<code php> 
 +<?php 
 + 
 +class foo extends ArrayObject { 
 + public function __construct($arr) { 
 + parent::__construct($arr); 
 +
 +
 + 
 +var_dump( (new foo( array(1, array(4, 5), 3) ))[1][0] ); // int(4) 
 + 
 +?> 
 +</code> 
 + 
 +<code php> 
 +<?php 
 + 
 +class foo { 
 + public $x = 1; 
 +
 + 
 +class bar { 
 + public $y = 'foo'; 
 +
 + 
 +$x = 'bar'; 
 + 
 +$bar = new bar; 
 + 
 +var_dump((new bar)->y);     // foo 
 +var_dump((new $x)->y);      // foo 
 +var_dump((new $bar->y)->x); // 1 
 + 
 +?> 
 +</code> 
 + 
 +<code php> 
 +<?php 
 + 
 +class bar { 
 + public $z; 
 +  
 + public function __construct() { 
 + $this->z = new stdclass; 
 +
 + public function getZ() { 
 + return $this->z; 
 +
 +
 + 
 +var_dump(clone (new bar)->z); 
 +var_dump(clone (new bar)->getZ()); 
 + 
 +?> 
 +</code> 
 + 
 +=== Without brackets === 
 + 
 +<code php> 
 +<?php 
 + 
 +class foo { 
 + public $x = 1; 
 +  
 + public function getX() { 
 + return $this->x; 
 +
 + public function setX($val) { 
 + $this->x = $val; 
 + return $this; 
 +
 +
 + 
 +$X = new foo->setX(10)->getX(); 
 +var_dump($X); // int(10) 
 + 
 +?> 
 +</code>
  
 <code php> <code php>
Line 40: Line 135:
  
 class foo { class foo {
- public $x = 1; + public function __construct() { 
-  + throw new Exception('foobar');
- public function getX() { +
- return $this->x; +
-+
- public function setX($val+
- $this->x = $val; +
- return $this;+
  }  }
 } }
  
-$X = new foo()->setX(10)->getX(); +try { 
-var_dump($X);+ $X = new foo->Inexistent(3)
 +} catch (Exception $e{ 
 + var_dump($e->getMessage()); // foobar 
 +}
  
 ?> ?>
 </code> </code>
  
-===== Proposal and Patch =====+===== Patch =====
  
 +  * http://felipe.ath.cx/diff/instance-method-call.patch (without brackets)
 +  * http://felipe.ath.cx/diff/instance-method-call-2.patch (with brackets)
 +  * http://felipe.ath.cx/diff/instance-method-call-3.patch (with brackets + array dereferencing)
  
 +===== Changelog =====
  
 +  * 08/04/2014 - Closed as "implemented"
 +  * 06/11/2011 - Implemented (Syntax 2) in [[http://git.php.net/?p=php-src.git;a=commit;h=ff48763f4b0fee906293815b42fe3e2a167702c8|ff48763f4b]] (PHP 5.4.0)
 +  * 26/11/2010 - Posted RFC on internals
 +  * 27/11/2010 - New syntax proposed
 +  * 29/11/2010 - Added array dereferencing support (e.g. %%(new foo)[0]%%)
rfc/instance-method-call.1290798257.txt.gz · Last modified: 2017/09/22 13:28 (external edit)