Create_function with default parameter values?

Ok I'm looking into the usage create_function

for what I need to do and I don't see a way to define default parameter values ​​with it. Is it possible? If so, what's the best approach for injecting parameters into a function create_function

in php? Perhaps using addslashes

?

Well, for example, I have a function like this:

function testing($param1 = 'blah', $param2 = array())
{
    if($param1 == 'blah')
        return $param1;
    else
    {
        $notblah = '';
        if (count($param2) >= 1)
        {
            foreach($param2 as $param)
                $notblah .= $param;

            return $notblah;
        }
        else
            return 'empty';
    }
}

      

So how could I use create_function

to accomplish the exact same action by adding parameters and their default values?

The point is that the parameters come from the TEXT file, as well as from the function itself. So wondering about the best approach for this, using create_function

and how exactly the string should be parsed.

Thanks:)

+2


a source to share


3 answers


Considering a function created with the create_function

following:

$func = create_function('$who', 'echo "Hello, $who!";');

      

You can call it like this:

$func('World');

      

And you get:

Hello, World!

      


Now, with a default value for the parameter, the code might look like this:

$func = create_function('$who="World"', 'echo "Hello, $who!";');

      



Note. I only added the default value for the parameter, in the first argument passed to create_function.

And then, by calling the new function:

$func();

      

I am still getting:

Hello, World!

      

i.e. the default value for the parameter is used.


Thus, default values ​​for parameters work with the create_function

same as for other functions: you just need to put the default value in the parameter list.

After that, on how to create a string containing the parameters and their values ​​... Several string concatenations, I suppose, without forgetting to avoid what needs to be escaped.

+2


a source


Do you want to create an anonymous function? create_function

used to create anonymous functions. Otherwise, you need to create a function as usual:

function name($parms)
{
   // your code here
}

      



If you want to use create_function

, here's a prototype:

$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
echo "New anonymous function: $newfunc\n";
echo $newfunc(2, M_E) . "\n";
// outputs
// New anonymous function: lambda_1
// ln(2) + ln(2.718281828459) = 1.6931471805599

      

0


a source


I have the same problem trying to pass an array to the generated callback function ... I think I will create a temporary variable ... This is ugly, but I better do what torture myself with a slash, my code is already cryptic enough as it is now ...

So, to illustrate:

global $tmp_someArray;
$tmp_someArray = $someArray;
$myCallback = create_function(
    '$arg1',
    '
          global $tmp_someArray;
          // do stuff with $tmp_someArray and $arg1....
          return($something);
    '
);

      

0


a source







All Articles