Table of Contents

Function Array Dereferencing (FAD)

Introduction

This RFC proposes the implementation of array dereferencing of method/function return.

Proposal and Patch

Patch: http://felipe.ath.cx/diff/array_dereference.diff

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: