SPL Startup Best Practices

In my server-side include_path, I have a link to the pear directory in '/ usr / share / pear /'. In my applications, I include files from the shared library living in '/ usr / share / pear / library /' s require_once 'library/file.php'.

I recently started using the spl autoloader, I noticed that in the bootloader function you need to define the logic with which you can include the file. My first way to do this is to try to include the file and suppress it with @, to see if it works, for example. @include 'library/file.php'however, I think that mainly because I read a lot about @bad practice, I decided to manually do this work myself, hacking get_include_pathwith help PATH_SEPARATORand seeing if the directory is what I want it to be, then execute file_existsand enable him.

Same:

function classLoader( $class ) {
    $paths = explode( PATH_SEPARATOR, get_include_path() );
    $file = SITE_PATH . 'classes' . DS . $class . '.Class.php';
    if ( file_exists( $file) == false ) 
    {
        $exists = false;
        foreach ( $paths as $path ) 
        {
            $tmp = $path . DS . 'library' . DS . 'classes' . DS . $class . '.Class.php';
            if ( file_exists ( $tmp ) ) 
            {
            $exists = true;
            $file = $tmp;
            }
        }
        if ( !$exists ) { return false; }
    }
    include $file;
}

spl_autoload_register('classLoader');

Did I go on the wrong route? Should I just do a business @include, or am I doing it somewhat in the right direction?

+5
2

, Habari , , .

, __autoload(), , , . , Dir glob():

$class_files = array(
  'user' => '/var/www/htdocs/system/classes/user.class.php',
);

$class_files[$class], . , , . ( , .)

, /. , Habari, , Habari __static() , , .

include_once() , @ , , .

+6

function autoload($class) {
    /* transform class name into filename ... */
    include $class;
}

@ ( / )

PHP: http://marc.info/?t=125787162200003&r=1&w=2

+1

All Articles