Last active
April 9, 2024 16:52
-
-
Save larbous/cda0e63f31c23ff108e2c94519783148 to your computer and use it in GitHub Desktop.
PHP 8 create_function replacement
This file contains 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 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