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

1

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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?
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.
Thank You Very Much, so I don't need to escape any characters in the parameters than right?
1 last thing, sorry, so I don't have to escape any characters in the parameters than right?
As long as your file contains a portion of valid-PHP code for parameters, I don't think you'd have to escape anything.
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.