PHP 7.2 Function create_function() is deprecated

前端 未结 4 1196
庸人自扰
庸人自扰 2020-11-22 07:18

I have used create_function in my application below.

$callbacks[$delimiter] = create_function(\'$matches\', \"return \'$delimiter\' . strtolower(\\$matches[1         


        
4条回答
  •  一生所求
    2020-11-22 07:45

    This Array of Anonymous functions worked for me, see code below:

    // This will be a dynamic name that could 
    // be used as a function like "namespace".
    $dynamic_name = 'my_dynamic_name';
    
    // Here's some variables that you could use in the scope of
    // your dynamic anonymous functions. 
    $outerVariable = 'If I need this varible, I can use it';
    $outerVariableTwo = 'If I need this varible, I can use it too!';
    
    // Create an array that we can later use and turn into 
    // and associative array with our new dynamic anonymous functions.
    $dynamicAnonFunctions = [];
    
    // Create the first dynamic function.
    $dynamicAnonFunctions[($dynamic_name."_func_one")] = function () use ($outerVariable, $dynamic_name) { 
        echo 'Running: function '.$dynamic_name .'_func_one()';
        echo '

    '; echo $outerVariable; echo '

    '; echo 'This works :)'; echo '

    '; }; // Create the second dynamic function $dynamicAnonFunctions[($dynamic_name."_func_two")] = function () use ($outerVariableTwo, $dynamic_name) { echo '- - - - - - - - - - - - - - - - - - - '; echo '

    '; echo 'Running: function '.$dynamic_name .'_func_two()'; echo '

    '; echo $outerVariableTwo; echo '

    '; echo 'This also works :)!'; echo '

    '; }; // Call the functions. $dynamicAnonFunctions[($dynamic_name."_func_one")](); $dynamicAnonFunctions[($dynamic_name."_func_two")](); // Halt execution. exit();

    Just copy this into your script file and you will see the output from the echo statements, then simply remap the function to your own will!

    Happy coding =)

提交回复
热议问题