rfc:functionarraydereferencing
Function Array Dereferencing (FAD)
- Version: 1.0
- Date: June 16, 2008 (added)
- Date: May 01, 2009 (declined)
- Date: June 07, 2010 (re-opened)
- Author: Philip Olson philip@php.net, Felipe Pena felipe@php.net
- Status: Implemented in PHP 5.4
Introduction
This RFC proposes the implementation of array dereferencing of method/function return.
Proposal and Patch
Examples
FAD adds the following:
<?php function fruit () { return array('a' => 'apple', 'b' => 'banana'); } echo fruit()['a']; // apple ?>
Which is a quasi-replacement for this:
<?php function fruit () { return array('a' => 'apple', 'b' => 'banana'); } $fruits = fruit(); echo $fruits['a']; ?>
Working with references:
<?php function &foo(&$foo) { return $foo; } $a = array(1); $b = foo($a)[0]; $b = 2; var_dump($b); // array(1) { [0]=> int(2) } ?>
Chaining:
<?php class foo { public $array = array(); public function __construct() { $this->array = array(1, 2.3); } public function bar() { return $this->array; } } $foo = new foo; var_dump($foo->bar()[1]); // float(2.3) $foo->array[] = $foo; var_dump($foo->bar()[2]->bar()[2]->array[0]); // int(1) ?>
Related discussion
The following discussions/threads have taken place:
- April 3, 2004: http://markmail.org/message/sbnrb7nekwy7xt3x
- November 9, 2006: http://markmail.org/message/5bv6mtd7zorni7gc
- June 3, 2010: http://markmail.org/message/3ancnjwqhax47n2j
- June 7, 2010: http://markmail.org/message/unplu5x3gjjjydwz [Patch proposal]
rfc/functionarraydereferencing.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1