开发者

Can temporary functions/macros be created in PHP?

Take a look at the following illustration:

// Trims input, fixes spaces and encodes bad glyphs. Also works with arrays.
function prepare_param($param)
{
    $retval = "";

    function prc($param)
    {
        $r = split(" ", trim($param));
        foreach($r as $i => $e)
            $r[$i] = urlencode($e);
        return join("+", $r);
    }

    // If input is an array
    if(is_array($param))
    {
        $retval = array();

        foreach($param as $e)
            $retval[] = prc($e);
    }
    // If input is a string
    else if(is_string($param))
    {
        return prc($param);
    }
    else throw开发者_运维知识库 new Exception("Invalid input! Expected String or Array.");
}

Obviously the function prc will now be declared globally, even though declared inside a function. Is there a way to follow this principle, creating a tiny function/macro inside another function as not to litter the global scope? The alternative would be to make a class with a private function, which seems like overkill for my use.

Any help appreciated


You probably want closures, which are anonymous functions.


If you have PHP 5.3, enter anonymous functions:

$prc = function($param)
{
    $r = split(" ", trim($param));
    foreach($r as $i => $e)
        $r[$i] = urlencode($e);
    return join("+", $r);
};

if(is_array($param))
{
    $retval = array();

    foreach($param as $e)
        $retval[] = $prc($e);
}
else if(is_string($param))
{
    return $prc($param);
}

In this case, $prc only lives in the scope of your prepare_param() function.


If you have access to >=PHP 5.3, you can use anonymous functions, and if not, you can use create_function.


If you don't have PHP 5.3, you can use the create_function function.


There are two ways to do so. The closures/anonymous functions are possible from PHP 5.3, and the oldschool way would be to use create_function() - which is quite fugly.

However in your case, you don't want either. There is no benefit in creating or recreating the function. You just need it once, as it does not depend on any initialization state. The idiom you should use is called "dererred definition" and possible in PHP with:

if (!function_exists("prc")) {
    function prc($param) {
        ...
    }
}

You should name it with its parent function as prefix however (e.g. prepare__prc) to avoid clashes and to signalize its internal use.

Oh, and btw it could also be simplified compacted into:

$param = join("+", array_map("urlencode", split(" ", trim($param))));


anonymous functions might be what you are looking for

<?php
$greet = function($name)
{
    printf("Hello %s\r\n", $name);
};

$greet('World');
$greet('PHP');
?>

If you don't use php 5.3 please be aware of the fact that the memory allocated by the "create_function()" function isn't released until the php process finishes. So if you create a lot of functions you might be running into issues.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜