在PHP中运行时删除一个函数 [英] Removing a function at runtime in PHP

查看:114
本文介绍了在PHP中运行时删除一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在递归函数中声明了一个如果阻止并且希望该功能仅在该if块中为有效。我不希望这个函数在这个块之外被调用。



我发现了 runkit_function_remove runkit 。有没有另外一种方法呢?



顺便说一句我只支持PHP 5.1.0。



编辑:我知道我的问题是hacky,但这是我想要做的确切的事情:

  if(function_exists ('get_magic_quotes_gpc')&& @get_magic_quotes_gpc())
{
函数stripslashes_deep($ value)
{
return is_array($ value)? array_map('stripslashes_deep',$ value):stripslashes($ value);
}

$ _POST = array_map('stripslashes_deep',$ _POST);
$ _GET = array_map('stripslashes_deep',$ _GET);
$ _COOKIE = array_map('stripslashes_deep',$ _COOKIE);
$ _REQUEST = array_map('stripslashes_deep',$ _REQUEST);

// runkit_function_remove('stripslashes_deep');

因为stripslashes_deep只会在魔术时出现 行情是开启的,当我完成它时,我想摆脱它。我不希望人们依赖一个并不总是 的函数。我希望现在更清楚。非巧克力解决方案的建议也受到欢迎! 关于函数的PHP手册


PHP中的所有函数和类都具有全局作用域 - 即使它们是在内部定义的,它们也可以在函数外调用,反之亦然。 [...] PHP不支持函数重载,也不可能取消定义或重定义先前声明的函数。

例外是通过runkit。但是,您可以将您的功能定义为匿名功能, a href =http://de2.php.net/manual/en/function.unset.php =noreferrer> unset 它之后你运行它,例如

  if(function_exists('get_magic_quotes_gpc')&& @get_magic_quotes_gpc())
$ fn = create_function('& $ v,$ k','$ v = stripslashes($ v);');
array_walk_recursive(array(& $ _ GET,& $ _ POST,& $ _ COOKIE,& $ _ REQUEST),$ fn);
unset($ fn);

$ / code>

有些评论者正确地指出(但现在PHP中不再有问题) ,你不能在其内部调用一个匿名函数。通过使用 array_walk_recursive ,你可以避开这个限制。就个人而言,我只会创建一个常规函数,而不会为删除它而烦恼。它不会伤害任何人。只要给它一个合适的名字,像 stripslashes_gpc_callback



注意: em>


I know this question seems hacky and weird, but is there a way to remove a function at runtime in PHP?

I have a recursive function declared in a "if" block and want that function to be "valid" only in that "if" block. I don't want this function to be callled outside this block.

I found out runkit_function_remove but runkit isn't enabled on my Web host. Is there another way to do that?

BTW I only support PHP 5.1.0.

Edit: I knew my question was hacky but here is the exact thing I want to do:

if (function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc())
{
    function stripslashes_deep($value)
    {
        return is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
    }

    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
    $_REQUEST = array_map('stripslashes_deep', $_REQUEST);

    //runkit_function_remove('stripslashes_deep');
}

Since "stripslashes_deep" will only live when Magic Quotes are ON, I wanted to get rid of it when I'm done with it. I don't want people to rely on a function that isn't always there. I hope it's clearer now. Non-hacky solution suggestions are welcome too!

解决方案

From the PHP Manual on Functions:

All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa. [...] PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions.

The exception is through runkit. However, you could define your function as an anonymous function and unset it after you ran it, e.g.

if (function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc())
    $fn = create_function('&$v, $k', '$v = stripslashes($v);'); 
    array_walk_recursive(array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST), $fn);
    unset($fn);
}

Some commentors correctly pointed out (but not an issue any longer in PHP nowadays), you cannot call an anonymous function inside itself. By using array_walk_recursive you can get around this limitation. Personally, I would just create a regular function and not bother about deleting it. It won't hurt anyone. Just give it a proper name, like stripslashes_gpc_callback.

Note: edited and condensed after comments

这篇关于在PHP中运行时删除一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆