How to unset a function definition just like we unset a variable?

后端 未结 5 885
小鲜肉
小鲜肉 2020-12-10 11:15

I want to define a function and unset it after its use just like we do with variables.

$a = \'something\';
unset($a);
echo $a; // outputs nothing


        
相关标签:
5条回答
  • 2020-12-10 11:27

    runkit_function_removeRemove a function definition http://php.net/manual/en/function.runkit-function-remove.php

    0 讨论(0)
  • 2020-12-10 11:44

    For the doubters, it makes perfect sense to want to delete a function. If you have a function that uses a lot of data just once at the beginning of your script then wanting to delete it makes sense.

    One such type of function may be the mobile/tablet detection function of a script or framework. It is only needed once and it is likely to take a significant amount of memory for a function that runs only once. If you have any doubt, check https://github.com/serbanghita/Mobile-Detect/blob/master/Mobile_Detect.json

    This said, there is a way around the problem without needing to delete the function. For what it is worth, make global all the arrays used by the function then at the end of the function unset them all. The function stays but the bulky data goes. Note that when you unset a variable, it is up to PHP's garbage collector to decide when the memory will be freed. And freeing the memory takes CPU cycles away from your application. I really cannot see any PHP script that would require unsetting a function.

    0 讨论(0)
  • 2020-12-10 11:45

    As of PHP 5.3, you can assign an anonymous function to a variable, then unset it:

    $upper = function($str) {
        return strtoupper($str);
    };
    
    echo $upper('test1');
    // outputs: TEST1
    
    unset($upper);
    
    echo $upper('test2');
    // Notice: Undefined variable: upper
    // Fatal error: Function name must be a string
    

    Before 5.3, you can do something similar with create_function()

    $func = create_function('$arg', 'return strtoupper($arg);');
    echo $func('test1');
    unset($func);
    
    $func2 = "\0lambda_1";
    echo $func2('test2.a'), "\n"; // Same results, this is the "unset" $func function
    
    echo $func('test2.b'); // Fatal Error
    
    0 讨论(0)
  • 2020-12-10 11:48

    A solution that avoids the requirement to unset a function and still use traditional functions....

    1. Place your function into a separate file
    2. Use require_once to call the file with the functions

    This way, if you are looping through code, you wont get an issue with calling the same function twice. I find this a neater solution and it makes it easier to call. you can then use url extensions to call groups of function or classes.

    Clearly this doesn't answer the request to remove, unset or destroy a function but it provides an alternate approach which may prove useful.

    0 讨论(0)
  • 2020-12-10 11:51

    From this question:

    You can use rename_function

    <?php
    rename_function('original_name', 'new_name' );
    ?>
    

    and then redeclare the original function.
    override_function may work too.

    They are part of the Zend PHP Debugger, if that doesn't work then you can always try something like:

    function function1(){
     echo "1";
    }
    
    function function2(){
     echo "2";
    }
    
    $i = "function2";
    $i(); //  displays 2;
    
    0 讨论(0)
提交回复
热议问题