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)
 
?>

The following discussions/threads have taken place:

rfc/functionarraydereferencing.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1