====== Request for Comments: Instance and method call/property access ====== * Version: 1.0 * Date: 2010-11-26 * Author: Felipe Pena * Status: Implemented (Syntax 2) ===== Introduction ===== 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: ===== 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 === y); // foo var_dump((new $x)->y); // foo var_dump((new $bar->y)->x); // 1 ?> z = new stdclass; } public function getZ() { return $this->z; } } var_dump(clone (new bar)->z); var_dump(clone (new bar)->getZ()); ?> === Without brackets === x; } public function setX($val) { $this->x = $val; return $this; } } $X = new foo->setX(10)->getX(); var_dump($X); // int(10) ?> bar()); // string(3) "foo" var_dump(new foo()->baz()->x); // string(7) "testing" var_dump(new foo()->baz()->baz()->bar()); // string(3) "foo" var_dump(new foo()->xyz()); // NULL new foo()->www(); // Fatal error: Call to undefined method foo::www() ?> Inexistent(3); } catch (Exception $e) { var_dump($e->getMessage()); // foobar } ?> ===== 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]%%)