Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickgalbraith/6a619576bfa2067334d365824878f051 to your computer and use it in GitHub Desktop.
Save patrickgalbraith/6a619576bfa2067334d365824878f051 to your computer and use it in GitHub Desktop.
Recreating create_function for PHP 7.2
<?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