Question

Ok, I'm looking into using create_function for what I need to do, and I don't see a way to define default parameter values with it. Is this possible? If so, what would be the best approach for inputting the params into the create_function function in php? Perhaps using addslashes?

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

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';
    }
}

Ok, so how would I use create_function to do the same thing, adding the parameters and their default values?

The thing is, the parameters are coming from a TEXT file, as well as the function itself. So, wondering on the best approach for this using create_function and how exactly the string should be parsed.

Thanks :)

Was it helpful?

Solution

Considering a function created with create_function this way :

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

You can call it like this :

$func('World');

And you'll get :

Hello, World!


Now, having a default value for a parameter, the code could 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, calling the new function :

$func();

I still get :

Hello, World!

i.e. the default value for the parameter has been used.


So, default values for parameters work with create_function just like they do for other functions : you just have to put the default value in the list of parameters.

After that, on how to create the string containing the parameters and their values... A couple of string concatenations, I suppose, without forgetting to escape what should be escaped.

OTHER TIPS

Do you want to create an anonymous function? The create_function is used to create the anonymous functions. Otherwise you need to create function normally like:

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

If you want to use the create_function, here is the 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

I'm having the same problem, trying to pass an array to a created callback function... I think I'll create a temporary variable... It's ugly but I have better to do then torture myself with slashes, my code is already cryptic enough the way 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);
    '
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top