15

I can't seem to find anything of this, and was wondering if it's possible to store a function or function reference as a value for an array element. For e.g.

array("someFunc" => &x(), "anotherFunc" => $this->anotherFunc())

Thanks!

nawfal
  • 70,104
  • 56
  • 326
  • 368
user784446
  • 489
  • 1
  • 9
  • 22
  • 1
    Possible duplicate of [Can you store a function in a PHP array?](http://stackoverflow.com/questions/1499862/can-you-store-a-function-in-a-php-array) – nawfal Nov 11 '15 at 11:29
  • `x()` is not a function or a reference to a function but a function call; i.e. the function is executed right now and the value it returns is stored in your array at key 'someFunc'. Read the PHP documentation about [functions](http://php.net/manual/en/language.functions.php) for more information (including the answer to your question). – axiac Nov 11 '15 at 11:37

4 Answers4

15

You can "reference" any function. A function reference is not a reference in the sense of "address in memory" or something. It's merely the name of the function.

<?php

$functions = array(
  'regular' => 'strlen',
  'class_function' => array('ClassName', 'functionName'),
  'object_method' => array($object, 'methodName'),
  'closure' => function($foo) {
    return $foo;
  },
);

// while this works
$functions['regular']();
// this doesn't
$functions['class_function']();

// to make this work across the board, you'll need either
call_user_func($functions['object_method'], $arg1, $arg2, $arg3);
// or
call_user_func_array($functions['object_method'], array($arg1, $arg2, $arg3));
rodneyrehm
  • 13,442
  • 1
  • 40
  • 56
  • Okay, thanks! So ultimately, I suppose I'm looking for a means for creating a pointer. I don't want to duplicate a function, but merely want a pointer to it such that I can modify and read the value to which the pointer refers. – user784446 Feb 25 '12 at 13:00
  • Damn, just realized that call_user_func was an actual member of PHP's function-method library! Thanks! – user784446 May 19 '12 at 07:53
  • 1
    @Wasim maybe should try a more "current" PHP version. 5.3 or above – rodneyrehm Jun 08 '13 at 09:00
5

PHP supports the concept of variable functions, so you can do something like this:

function foo() { echo "bar"; }
$array = array('fun' => 'foo');
$array['fun']();

Yout can check more examples in manual.

Paulo Rodrigues
  • 5,273
  • 7
  • 35
  • 58
  • 1
    Thanks, was thinking of doing that as a solution. But the docs also states that an element's value can be of any type, so why can't functions be used? I can use "element"=>object(), but don't know of what can be done with this. – user784446 Feb 25 '12 at 12:09
5

check out PHP's call_user_func. consider the below example.

consider two functions

function a($param)
{
    return $param;
}

function b($param)
{
    return $param;
}


$array = array('a' => 'first function param', 'b' => 'second function param');

now if you want to execute all the function in a sequence you can do it with a loop.

foreach($array as $functionName => $param) {
    call_user_func($functioName, $param);
}

plus array can hold any data type, be it function call, nested arrays, object, string, integer etc. etc.

Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207
5

Yes, you can:

$array = array(
    'func' => function($var) { return $var * 2; },
);
var_dump($array['func'](2));

This does, of course, require PHP anonymous function support, which arrived with PHP version 5.3.0. This is going to leave you with quite unreadable code though.

Treffynnon
  • 21,365
  • 6
  • 65
  • 98