Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How do I pass a variable to create_function() ?

I have something like this:

function my_hook_function(){
  $var = 10;
  apply_filters('hook', $var);
  return $var;
}


$variable = 5;
$function = create_function('', 'return $variable;');

add_filter('hook', $function); 

echo my_hook_function();

but it doesn't work :( theoretically the output should be 5

add_filter() is a wordpress function that allows you to change stuff around :)

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

There are two problems here. The first one is that you need to tell php what parameters get passed to the 'created' function, or reference them as a global inside of the body. The second is that you are expecting $var to be modified by the created function, but you are not passing it a reference. the created function simply returns the new variable and you do nothing with it.

function my_hook_function(){
  $var = 10;
  $var = apply_filters('hook', $var);
  return $var;
}

/* This will return 5 by dividing the passed value by 2 and returning the result */
$function = create_function('$variable', 'return $variable/2;');    
add_filter('hook', $function);
echo my_hook_function();

/* This will return 5 by referencing the global $variable */
$variable = 5;
$function = create_function('', 'global $variable; return $variable;');
add_filter('hook', $function);
echo my_hook_function();

Note that if you run this code exactly like this that both of the filters will be added to the 'hook' hook.

share|improve this answer
add comment
$outsideVarName = 3434;
$function = create_function('$insideFunctionVarName', 'return $insideFunctionVarName;');
echo $function($outsideVarName);
share|improve this answer
add comment

Reading the manual for create_function the usage for passing $variable to the function would be as follows:

$variable = 3434;
$function = create_function('$v', 'return $v;');

echo $function($variable); 

EDIT

Changed $variable inside the create_function call to make it a bit clearer of proper usage and avoid confusion.

UPDATE

Given the comment below, here is an updated version:

$variable = 3434;
$function = create_function('$v', 'return $v;');

function myTest($function, $var) {
    echo $function($var);
}

myTest($function, $variable); // should echo 3434

Not sure if that is what you want, I will refrain from guessing further till you show the actual context you are working in.

Update 2

Doing some research, is there a reason you do not just use it in this manner:

add_filter('something', create_function('$v', 'return $v;'));

From what I could find that should work...

share|improve this answer
 
hm.. the problem is that i can call $function like that :( the created function is passed as a argument to another function, and needs to be a string... –  Alex Sep 2 '10 at 22:26
 
Show your full code please or at least the context you are working in (IE the function you are passing $function to). I cannot accurately diagnose an issue if you only give me 1/4 of the problem. –  Brad F Jacobs Sep 2 '10 at 22:28
 
ok, edited. it's wordpress-related, but I don't think wp has anything to do with the errors i get –  Alex Sep 2 '10 at 22:35
 
Well it depends on how add_hook uses the variable $function. Chances are it thinks that $function should be the name of a function and not an actual function. –  Brad F Jacobs Sep 2 '10 at 22:37
 
yes, the 2nd paramater should be the function name, but isn't that that $function is ? –  Alex Sep 2 '10 at 22:39
show 3 more comments

I think what you really want is

$function = create_function('', "return $variable;");

or

$function = create_function('', 'return ' . $variable . ';');

i.e. you want to capture the value of the variable into the function when created. Since a number converted to a string is the same as its source representation, simply interpolating it into the source of the function is sufficient. For strings and arrays, some quoting will be necessary.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.