Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP pass function name as param then call the function?

I need to pass a function as a parameter to another function and then call the passed function from within the function...This is probably easier for me to explain in code..I basically want to do something like this:

function ($functionToBeCalled) {    call($functionToBeCalled,additional_params); } 

Is there a way to do that.. I am using PHP 4.3.9

Thanks!

like image 274
JD Isaacks Avatar asked Mar 09 '09 19:03

JD Isaacks


People also ask

Can I pass a function as a parameter in PHP?

There is another type of function known as PHP Parameterized functions, these are the functions with pre defined parameters. You'll pass any number of parameters inside a function. These passed parameters act as variables in your function. They are declared inside the brackets, after the function name.

How do you pass a function to another function in PHP?

Just to add to the others, you can pass a function name: function someFunc($a) { echo $a; } function callFunc($name) { $name('funky! '); } callFunc('someFunc');

Are PHP functions callable?

Definition and Usage. The is_callable() function checks whether the contents of a variable can be called as a function or not. This function returns true (1) if the variable is callable, otherwise it returns false/nothing.


1 Answers

I think you are looking for call_user_func.

An example from the PHP Manual:

<?php function barber($type) {     echo "You wanted a $type haircut, no problem"; } call_user_func('barber', "mushroom"); call_user_func('barber', "shave"); ?> 
like image 172
Paolo Bergantino Avatar answered Sep 23 '22 16:09

Paolo Bergantino