# RFC: Function array dereferencing

- Status: Implemented (PHP 5.4)
- Authors: Philip Olson philip@php.net, Felipe Pena felipe@php.net
- Date: June 16, 2008 (added)
- Version: 1.0
- Original: https://wiki.php.net/rfc/functionarraydereferencing
- Page: https://externals.io/rfc/functionarraydereferencing
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Discussions on the mailing list

- [\[RFC\] Array Dereferencing](https://externals.io/message/48671.md) — started on 2010-06-07, 21 messages

---

## 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](http://felipe.ath.cx/diff/array_dereference.diff)

## Examples

FAD adds the following:

```php
<?php
 
function fruit () {
  return array('a' => 'apple', 'b' => 'banana');
}
 
echo fruit()['a']; // apple
 
?>
```

Which is a quasi-replacement for this:

```php
<?php
 
function fruit () {
  return array('a' => 'apple', 'b' => 'banana');
}
 
$fruits = fruit();
echo $fruits['a'];
 
?>
```

Working with references:

```php
<?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
<?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\]
