Take function as parameter in PHP

I was wondering if it is possible or not to pass a function as a parameter in PHP; I want something like when you are programming in JS:

object.exampleMethod(function(){ // some stuff to execute }); 

I want to execute this function somewhere in the exampleMethod method. Is this possible in PHP?

+61
function php
Apr 23 '10 at 16:56
source share
8 answers

Perhaps if you are using PHP 5.3.0 or higher.

See Anonymous Functions in the manual.

In your case, you should define exampleMethod as follows:

 function exampleMethod($anonFunc) { //execute anonymous function $anonFunc(); } 
+96
Apr 23 2018-10-23T00:
source share

To add to the rest, you can pass a function name:

 function someFunc($a) { echo $a; } function callFunc($name) { $name('funky!'); } callFunc('someFunc'); 

This will work in PHP4.

+31
Apr 23 '10 at 17:46
source share

You can also use create_function to create a function as a variable and pass it. Although, I like the feeling of anonymous functions . Go zombie.

+14
Apr 23 '10 at 17:01
source share

Just enter the code like this:

 function example($anon) { $anon(); } example(function(){ // some codes here }); 

it would be great if you could come up with something like this (inspired by Laravel Illuminate):

 Object::method("param_1", function($param){ $param->something(); }); 
+7
Jul 01 '13 at 9:20
source share

A simple example using the class:

 class test { public function works($other_parameter, $function_as_parameter) { return $function_as_parameter($other_parameter) ; } } $obj = new test() ; echo $obj->works('working well',function($other_parameter){ return $other_parameter; }); 
+2
Nov 06 '14 at 11:13
source share

PHP VERSION> = 5.3.0

Example 1: basic

 function test($test_param, $my_function) { return $my_function($test_param); } test("param", function($param) { echo $param; }); //will echo "param" 

Example 2: std object

 $obj = new stdClass(); $obj->test = function ($test_param, $my_function) { return $my_function($test_param); }; $test = $obj->test; $test("param", function($param) { echo $param; }); 

Example 3: calling a non-static class

 class obj{ public function test($test_param, $my_function) { return $my_function($test_param); } } $obj = new obj(); $obj->test("param", function($param) { echo $param; }); 

Example 4: calling a static class

 class obj { public static function test($test_param, $my_function) { return $my_function($test_param); } } obj::test("param", function($param) { echo $param; }); 
0
May 21 '16 at 1:31
source share

Tested for PHP 5.3

As I see here, an anonymous function can help you: http://php.net/manual/en/functions.anonymous.php

What you may need, and he did not say before that how to pass a function without its wrapper inside the built-in function . As you will see later, you need to pass the name of the function written in the string as a parameter, check its "callability" and then call it.

Verification Function:

 if( is_callable( $string_function_name ) ){ /*perform the call*/ } 

Then, to call it, use this piece of code (if you need parameters, put them in an array), look at: http://php.net/manual/en/function.call-user-func.php

 call_user_func_array( "string_holding_the_name_of_your_function", $arrayOfParameters ); 

as follows (similar, without parameters):

  function funToBeCalled(){ print("----------------------i'm here"); } function wrapCaller($fun){ if( is_callable($fun)){ print("called"); call_user_func($fun); }else{ print($fun." not called"); } } wrapCaller("funToBeCalled"); wrapCaller("cannot call me"); 

Here's a class explaining how to do something like this:

 <?php class HolderValuesOrFunctionsAsString{ private $functions = array(); private $vars = array(); function __set($name,$data){ if(is_callable($data)) $this->functions[$name] = $data; else $this->vars[$name] = $data; } function __get($name){ $t = $this->vars[$name]; if(isset($t)) return $t; else{ $t = $this->$functions[$name]; if( isset($t)) return $t; } } function __call($method,$args=null){ $fun = $this->functions[$method]; if(isset($fun)){ call_user_func_array($fun,$args); } else { // error out print("ERROR: Funciton not found: ". $method); } } } ?> 

and usage example

 <?php /*create a sample function*/ function sayHello($some = "all"){ ?> <br>hello to <?=$some?><br> <?php } $obj = new HolderValuesOrFunctionsAsString; /*do the assignement*/ $obj->justPrintSomething = 'sayHello'; /*note that the given "sayHello" it a string ! */ /*now call it*/ $obj->justPrintSomething(); /*will print: "hello to all" and a break-line, for html purpose*/ /*if the string assigned is not denoting a defined method , it treat as a simple value*/ $obj->justPrintSomething = 'thisFunctionJustNotExistsLOL'; echo $obj->justPrintSomething; /*what do you expect to print? just that string*/ /*NB: "justPrintSomething" is treated as a variable now! as the __set override specify"*/ /*after the assignement, the what is the function destiny assigned before ? It still works, because it held on a different array*/ $obj->justPrintSomething("Jack Sparrow"); /*You can use that "variable", ie "justPrintSomething", in both ways !! so you can call "justPrintSomething" passing itself as a parameter*/ $obj->justPrintSomething( $obj->justPrintSomething ); /*prints: "hello to thisFunctionJustNotExistsLOL" and a break-line*/ /*in fact, "justPrintSomething" it a name used to identify both a value (into the dictionary of values) or a function-name (into the dictionary of functions)*/ ?> 
0
Nov 30 '16 at 0:38
source share

According to @zombat's answer, it's better to check anonymous functions first:

 function exampleMethod($anonFunc) { //execute anonymous function if (is_callable($anonFunc)) { $anonFunc(); } } 
0
Oct 11 '17 at 8:43 on
source share



All Articles