Created
September 7, 2018 04:49
-
-
Save patrickgalbraith/6a619576bfa2067334d365824878f051 to your computer and use it in GitHub Desktop.
Recreating create_function for PHP 7.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// WARNING - You should always ensure that the code cannot be updated | |
// using an anonymous function before using this | |
function createCallback($paramList, $code) { | |
$callback = create_function($paramList, $code); | |
return $callback; | |
} | |
function createCallbackPHP72($paramList, $code) { | |
$callback = "create_function_" . uniqid() . mt_rand(); | |
eval("function $callback($paramList) { $code }"); | |
return $callback; | |
} | |
$cb1 = createCallback('$a,$b', 'echo $a; echo $b;'); | |
$cb2 = createCallbackPHP72('$a,$b', 'echo $a; echo $b;'); | |
$cb1(1, 2); | |
$cb2(3, 4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment