views:

41

answers:

3

Hi

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 :)

+2  A: 

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...

Brad F Jacobs
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
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
ok, edited. it's wordpress-related, but I don't think wp has anything to do with the errors i get
Alex
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
yes, the 2nd paramater should be the function name, but isn't that that $function is ?
Alex
A var_dump showed this: `var_dump($function); string(9) "lambda_1" ` so no, it is not the name of the function, I bet it is some sort of a reference, you can try calling `lambda_1($f, $var);` but it will just return a function not found.
Brad F Jacobs
yes, i can't use create_function inside add_filter, because I need to add a remove_filter($function) later, and that needs the function name, which I don't have :)
Alex
So why not just make it a real function? From my research, I have not seen a way that makes it possible for you to use it as described, unfortunately.
Brad F Jacobs
+1  A: 
$outsideVarName = 3434;
$function = create_function('$insideFunctionVarName', 'return $insideFunctionVarName;');
echo $function($outsideVarName);
aularon
+1  A: 

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.

spuriousdata