Skip to content

Instantly share code, notes, and snippets.

@larbous
Last active April 9, 2024 16:52
Show Gist options
  • Save larbous/cda0e63f31c23ff108e2c94519783148 to your computer and use it in GitHub Desktop.
Save larbous/cda0e63f31c23ff108e2c94519783148 to your computer and use it in GitHub Desktop.
PHP 8 create_function replacement
/*
* PHP 8 create_function replacement.
* Use apenas como um paliativo para o sistema antigo voltar
* a funcionar e atualize em seguida.
* coloque este trecho dentro do arquivo functions.php do tema ativo.
*/
if ( ! function_exists( "create_function" ) ) {
function create_function( $arg, $body ) {
static $cache = array();
static $max_cache_size = 64;
static $sorter;
if ( $sorter === NULL ) {
$sorter = function( $a, $b ) {
if ( $a->hits == $b->hits ) {
return 0;
}
return ($a->hits < $b->hits) ? 1 : -1;
};
}
$crc = crc32($arg . "\\x00" . $body);
if (isset($cache[$crc])) {
++$cache[$crc][1];
return $cache[$crc][0];
}
if ( sizeof($cache) >= $max_cache_size ) {
uasort($cache, $sorter);
array_pop($cache);
}
$cache[$crc] = array( $cb = eval('return function('.$arg.'){'.$body.'};'), 0 );
return $cb;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment