Does the exists () function cache its queries?

I am wondering if function_exists () internally cache their requests?

+4
source share
2 answers

No, it is not. It just checks to see if a function is defined in the function table. Simple enough.

However, the Zend Opcache extension can optimize some callsfunction_exists() and some other functions, which in some cases can be evaluated at compile time. (It only optimizes calls function_exists()where the function is defined internally by PHP or by extension.)

+5
source

, , :

function function_exists_cached($function_name)
{
    static $cache = array();
    if(isset($cache[$function_name]))
        return $cache[$function_name];
    return ($cache[$function_name] = \function_exists($function_name));

}
$funcs = \array_shift(\get_defined_functions());
$a = new \core\utiles\loadTime;
$times = $tot = 0;
$a->start();
/**
 * None Cached: No cache used here
 **/
for($index = 0; $index<count($funcs); $index++)
{
    foreach($funcs as $func)
    {
        $times++;
        if(\function_exists($func)) ;
    }
}
$s = $a->stop();
$tot+=$s;
echo "<b>None Cached : </b><b><span style='color:green'>#$times</span></b> function check took : $s seconds<br />";
$b = new \core\utiles\loadTime;
$times = 0;
$b->start(); 
/**
 * Fly Cached: An inline cache used here
 **/
static $func_check = array();
for($index = 0; $index<count($funcs); $index++)
{
    foreach($funcs as $func)
    {
        $times++;
        if(!isset($func_check[$func]))
        {
            if(\function_exists($func)) ;
            $func_check[$func] = 1;
        }
        else $func_check[$func];
    }
}
$s = $b->stop();
$tot+=$s;
echo "<b>Fly Cached : </b><b><span style='color:green'>#$times</span></b> function check took : $s seconds<br />";
$c = new \core\utiles\loadTime;
$times = 0;
$c->start();
/**
 * Function Cached: Has the same logic like Fly Cached section, 
 * But implemented in a function named function_exists_cached()
 **/
for($index = 0; $index<count($funcs); $index++)
{
    foreach($funcs as $func)
    {
        $times++;
        if(\function_exists_cached($func)) ;
    }
}
$s = $c->stop();
$tot+=$s;
echo "<b>Function Cached : </b><b><span style='color:green'>#$times</span></b> function check took : $s seconds<br />";
echo "Total time : $tot seconds";

:

: # 2365444 : 0.69758
Fly Cached: # 2365444 : 0,5307
: # 2365444 : 1.02575
: 2.25403


, , function_exists_cached($function_name) Fly Cached, ?
0

Source: https://habr.com/ru/post/1525738/


All Articles