PHP pass function name as parameter then call function?

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

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

Is there any way to do this. I am using PHP 4.3.9

Thank!

+43
function php parameters
Mar 09 '09 at 19:58
source share
7 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"); ?> 
+77
Mar 09 '09 at 20:00
source share
 function foo($function) { $function(" World"); } function bar($params) { echo "Hello".$params; } $variable = 'bar'; foo($variable); 

Alternatively, you can do it this way. See function variables .

+26
Mar 09 '09 at 20:04
source share

In php it is very simple.

 <?php function here() { print 'here'; } function dynamo($name) { $name(); } //Will work dynamo('here'); //Will fail dynamo('not_here'); 
+25
Mar 09 '09 at 20:13
source share

You can also use call_user_func_array() . It allows you to pass an array of parameters as a second parameter, so you do not need to know exactly how many variables you are passing.

+11
May 7 '09 at 23:06
source share

I know the original question asked about PHP 4.3, but now it's a few years later, and I just wanted to uphold my preferred way of doing this in PHP 5.3 or later.

PHP 5.3+ now includes support for anonymous functions (closures) , so you can use some standard methods of functional programming, for example, in languages ​​such as JavaScript and Ruby (with a few caveats). Rewriting the call_user_func example above in the “close style” will look like I think is more elegant:

 $barber = function($type) { echo "You wanted a $type haircut, no problem\n"; }; $barber('mushroom'); $barber('shave'); 

Obviously, in this example you are not buying a lot - strength and flexibility come when you pass these anonymous functions to other functions (as in the original question). So you can do something like:

 $barber_cost = function($quantity) { return $quantity * 15; }; $candy_shop_cost = function($quantity) { return $quantity * 4.50; // It Moonstruck chocolate, ok? }; function get_cost($cost_fn, $quantity) { return $cost_fn($quantity); } echo '3 haircuts cost $' . get_cost($barber_cost, 3) . "\n"; echo '6 candies cost $' . get_cost($candy_shop_cost, 6) . "\n"; 

This can be done with call_user_func, of course, but I find this syntax clearer, especially after namespaces and member variables are involved.

One caveat: I will be the first to admit that I don’t know exactly what is happening here, but you cannot always cause a closure contained in a member or static variable, and possibly in some other cases. But reassigning it to a local variable will allow it to be called. So, for example, this will give you an error message:

 $some_value = \SomeNamespace\SomeClass::$closure($arg1, $arg2); 

But this is a simple solution to the problem:

 $the_closure = \SomeNamespace\SomeClass::$closure; $some_value = $the_closure($arg1, $arg2); 
+11
Dec 06 '13 at 18:20
source share

If you need to pass a function with a parameter as a parameter, you can try this:

 function foo ($param1){ return $param1; } function bar ($foo_function, $foo_param){ echo $foo_function($foo_param); } //call function bar bar('foo', 'Hi there'); //this will print: 'Hi there' 

phpfiddle example

Hope this will be helpful ...

+6
May 16 '16 at 8:59
source share

If you want to do this inside a PHP class, take a look at this code:

 // Create a sample class class Sample { // Our class displays 2 lists, one for images and one for paragraphs function __construct( $args ) { $images = $args['images']; $items = $args['items']; ?> <div> <?php // Display a list of images $this->loop( $images, 'image' ); // notice how we pass the name of the function as a string // Display a list of paragraphs $this->loop( $items, 'content' ); // notice how we pass the name of the function as a string ?> </div> <?php } // Reuse the loop function loop( $items, $type ) { // if there are items if ( $items ) { // iterate through each one foreach ( $items as $item ) { // pass the current item to the function $this->$type( $item ); // becomes $this->image // becomes $this->content } } } // Display a single image function image( $item ) { ?> <img src="<?php echo $item['url']; ?>"> <?php } // Display a single paragraph function content( $item ) { ?> <p><?php echo $item; ?></p> <?php } } // Create 2 sample arrays $images = array( 'image-1.jpg', 'image-2.jpg', 'image-3.jpg' ); $items = array( 'sample one', 'sample two', 'sample three' ); // Create a sample object to pass my arrays to Sample $elements = { 'images' => $images, 'items' => $items } // Create an Instance of Sample and pass the $elements as arguments new Sample( $elements ); 
0
Nov 20 '18 at 2:28
source share



All Articles