# RFC: Indirect method call by array variable

- Status: Implemented (PHP 5.4)
- Authors: Felipe Pena felipe@php.net
- Date: 2011-06-05
- Version: 1.0
- Original: https://wiki.php.net/rfc/indirect-method-call-by-array-var
- Page: https://externals.io/rfc/indirect-method-call-by-array-var
- 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

No thread found.

---

## Introduction

Reading our bug tracker I noticed a good feature request (<http://bugs.php.net/bug.php?id=47160>) from 2009 which points to an interesting feature that I think makes sense for us, since we are now working with $f() using objects and strings, and the array('class', 'method') is an old known for call\_user\_func()-like functions.

The array to be a valid callback should be a 2-element array, and it must be for the first element object/string and for the second string only. (just like our zend\_is\_callable() check and opcodes related to init call)

## Example

```php
<?php
 
class Hello {
   public function world($x) {
      echo "Hello, $x\n"; return $this;
   }
}
 
 
$f = array('Hello','world');
var_dump($f('you')); // Hello, you
 
?>
```

Checking for callback, nowadays already recognizes array('class', 'method') as valid callback, so it will facility the indirect call through a variable:

```php
<?php
 
 
class Hello {
   static public function world($x) {
     echo "Hello, $x\n";
   }
}
 
 
function hello_world($x) {
   echo "Hello, $x\n";
}
 
 
$callbacks = array(
   array('Hello', 'world'),
   function ($x) { echo "Hello, $x\n"; },
   'hello_world'
);
 
 
foreach ($callbacks as $k => $callback) {
   if (is_callable($callback)) {
     $callback($k);
   }
}
 
?>
```

Output:

```
Hello, 0
Hello, 1
Hello, 2
```

## Patch

1. <http://felipe.ath.cx/diff/fr47160.diff>
