Removing a function at runtime in PHP (without the runkit extension)

前端 未结 9 1470
逝去的感伤
逝去的感伤 2020-12-09 16:33

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 wan

9条回答
  •  孤街浪徒
    2020-12-09 16:51

    I too see very little reason to let a function "live" or "not live" depending on a condition, but to answer the question, it's sort of possible using anonymous functions. @Gordon already outlined how it's done. Starting with PHP 5.3.0, you can also use anonymous functions as follows. (There is no functional difference to the create_function() approach.)

    if (function_exists('get_magic_quotes_gpc') && @get_magic_quotes_gpc())
    {
        $stripslashes_deep = function($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);
    
        unset ($stripslashes_deep);
    }
    

提交回复
热议问题