Code

Coding, Programming & Algorithms, Tips, Tweaks & Hacks
Search

Sponsored Ads

Always check for existence of a file in spl_autoload_register's function call

PHP
<?php
function my_autoloader_1($className)
{    
    $filename = 'test1/'.$className.'.php';
    if (file_exists($filename))
    {
        require_once $filename;
    }
    else
    {
        return false;
    }
}

# foo.php is in test2
function my_autoloader_2($className)
{    
    $filename = 'test2/'.$className.'.php';
    if (file_exists($filename))
    {
        require_once $filename;
    }
    else
    {
        return false;
    }    
}

spl_autoload_register('my_autoloader_1');
spl_autoload_register('my_autoloader_2');
 
$a = new foo();
?>
PHP 7
Vanakkam !

0 comments: