views:

88

answers:

2

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

A: 

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
Sarfraz
+2  A: 

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.

Pascal MARTIN
Ok, thank you, this is coming from a text file, so it could be like this for the parameters: `$who = 'World'`, would this work in there also, or is there a function to substitute single quotes for double quotes? Or should I use `addslashes` for this?
SoLoGHoST
If you get this into a `$params` variable, you could use that `$params` variable as first parameter to `create_function` : its content is exactly what should be used there.
Pascal MARTIN
Thank You Very Much, so I don't need to escape any characters in the parameters than right?
SoLoGHoST
YOu're welcome :-) Have fun !
Pascal MARTIN
1 last thing, sorry, so I don't have to escape any characters in the parameters than right?
SoLoGHoST
As long as your file contains a portion of valid-PHP code for parameters, I don't think you'd have to escape anything.
Pascal MARTIN