PHP Pass Function Name as Param Then Call the Function

PHP pass function name as param then call the function?

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");
?>

Pass a function name as parameter in a function

function test($str, $func){
return call_user_func($func,$str);
}

Use it like this

$asd = test("ASD","strtolower");

Accept function as parameter in PHP

It's possible if you are using PHP 5.3.0 or higher.

See Anonymous Functions in the manual.

In your case, you would define exampleMethod like this:

function exampleMethod($anonFunc) {
//execute anonymous function
$anonFunc();
}

PHP - Can I pass a function name as a function argument?

To answer your question first: No, you can't (without resorting to evilCode) pass a function name as a parameter.
But: What you want to archive is a poster-child-issue for an object oriented approach using inheritance.

You'd need a base-class:

class BaseClass
{
function __construct($db) {
$this->db = db;
}
}

and your implementations :

class MyClass extends BaseClass
{
function __construct($db) {
parent::__contruct($db);
$this->userDAO = DAO_DBrecord::createUserDAO($this->db);
}
}

Just for the record: the evilCode would have been
a) you could encapsulate your function in a create_function that can be used as an argument.
b) you could pass the function name as a string to your function and then pass it to eval in the receiving function.

But remember: When eval or create_function looks like the answer you're probably asking the wrong questions!

See: related question

PHP executed function as function's parameter

So with the understanding of anonymous functions (Thanks Lucas Meine ), I found that you can execute a function directly by surrounding it with brackets, followed by parameter brackets that will be empty or not depending on whether you want to give the lower level function some of your variables out of the main context.

In all following examples the output of var_dump is 42.

example without variable parameters

// the function that should later be called
function myFunction($test){
var_dump($test);
}

// function call with a executed function as parameter
myFunction((function(){
return 42;
})());

example with variable parameters

$someVar = 42;

function myFunction($test){
var_dump($test);
}

myFunction((function($tmp){
return $tmp;
})($someVar));

or with the use statement

$someVar = 42;

function myFunction($test){
var_dump($test);
}

myFunction((function() use ($someVar){
return $someVar;
})());

pass variable to function using same name

No there is not problem with doing this except some naming confusion. Because,

$variable = "value"; // this declaration belong to a global scope

// but $variable passing through as argument, makes it scope within that function declaration only
function myFunction($variable) {
return $variable;
}

You might have expect that if you call the function like myFunction() i.e. without any parameter, it will return you "value", but absolutely not, cause $variable passed as argument is not the global one.

But if you try to use the global $variable within the myFunction, then it will cause some conflict, that is why no one do this.

Pass function and parameters as parameters to intermediary function which then calls named function

Change the third parameter to an array:

$posts = transitize('transientname', 'functionname', array(4, false, array($post->projectsiteid)));

Change this line to use call_user_func_array:

$functionresult = ($args) ? call_user_func_array($functionname, $args) : $functionname();


Related Topics



Leave a reply



Submit