Lesser known PHP functions – call_user_func_array
I wanted to start a series on lesser known PHP functions and call_user_func_array came to me as a good starter topic. In my experience most programmers who use this PHP function are building or working with frameworks. Also, don’t get me wrong, this function may be used more often in your working environment than most, so it may or may not be lesser known to you. If you would like to suggest other ones or write an article, by all means let me know!
Simply stated call_user_func_array allows you to call a user function given with an array of parameters. Basically we use this function to mimic some code that many of us write on a daily basis. For example:
function foo($bar, $baz) {
print “$bar and $baz”;
}
foo('salt', 'pepper);
The above example we all have worked with. It’s a simple user created function called foo with two function parameters we’ve named bar and baz. We can call the foo function, and as long as we are passing two parameters in our call the function will take those parameters and execute the commands inside the function.
What would happen if we only had the function name available to us at run time? The above examples assume we know what we are running at all times, but in the case of some frameworks the functions or classes loaded depend on what page the website is on for example.
Using frameworks as our example lets say we land on the about page. We’ve set up our code to tell us at run time what page we are opening. For example:
function about($foo) {
// do your code here
}
// We are assuming $page == about
call_user_func_array("$page", array("bar"));
The above code would load the about function in our procedural PHP code. Perhaps we’ve created a more advanced framework and we are investing in Object Oriented Programing(OOP). How can we work with call_user_func_array and classes and their methods?
class about {
public static function foo($bar, $baz) {
// do your code here
print “$bar <br /> $baz”;
}
}
call_user_func_array( array('about', 'foo'), array('three', 'four'));
A big thank you to Rune Kaagaard, I took my code for granted and forgot about how I was calling a non static function in static content. In most of my code I use this function like the example Rune provided below, but I thought I would make some changes to simplify the code for the article. I should have known I was complicating it more.
class about {
function foo($bar, $baz) {
// do your code here
print “$bar <br /> $baz”;
}
}
$about = new about();
call_user_func_array( array($about, 'foo'), array('three', 'four'));
And, that about sums it up for a quick lesson on call_user_func_array, In the future I’ll put together a quick tutorial about how to use this function to build a quick page loading framework for you guys.
If you like this article, please use the buttons below to share it or leave me a comment and let me know your thoughts – Shawn








![Validate my RSS feed [Valid RSS]](http://www.unemployeddeveloper.com/wp-content/themes/ud2/images/valid-rss.png)
[...] Lesser known PHP functions – call_user_func_array | Unemployed … [...]
Variants of this would include call_user_func and using variable variables. Examples (maybe weird blog syntax comming up):
call_user_func(’foo’, 42, 43, 44);
$var = ‘foo’;
$$var(42,43,44);
and the evil (VERY SLOW):
eval(’foo(42,43,44);’);
I believe your class example would give a E_STRICT error because you are trying to call a non static function in a static content.
So you should declare the function like this:
public static function foo($bar, $baz).
If what you want is to use a class instance the syntax is:
foz $baz, $bar”;
}
}
$about = new about();
call_user_func_array( array($about, ‘foo’), array(’three’, ‘four’));
Take Care!
Display of last example is broken.
@Rune – I completely forgot about the public static declaration. In my code(s) I have it set up both ways that you describe. Good catch, I’ll change it in the writeup.
*“ I am very thankful to this topic because it really gives useful information ‘~;
I have not thought about Lesser known PHP functions – call_user_func_array | Unemployed Developer that way. I need to mention I adore your website. by the way! It is best to add certain additional photos and videos on http://www.unemployeddeveloper.com/239_lesser-known-php-functions-call_user_func_array, like that it will be considerably even more interesting for next guests. Desire that will assist!