Autoload classes from different folders

前端 未结 12 965
不知归路
不知归路 2020-11-28 19:35

This is how I autoload all the classes in my controllers folder,

# auto load controller classes
    function __autoload($class_name) 
    {
             


        
12条回答
  •  情深已故
    2020-11-28 20:07

    I have to mention something about "good" autoload scripts and code structure, so read the following CAREFULLY


    Keep in Mind:

    • Classname === Filename
    • Only ONE class per file

    e.g: Example.php contains

    class Example {}
    
    • Namespace === Directory structure

    e.g: /Path1/Path2/Example.php matches

    namespace Path1\Path2;
    class Example {}
    
    • There SHOULD be a Root-Namespace to avoid collisions

    e.g: /Path1/Path2/Example.php with root:

    namespace APP\Path1\Path2;
    class Example {}
    
    • NEVER use manually defined path or directory lists, just point the loader to the top most directory
    • Keep the loader AS FAST AS POSSIBLE (because including a file is expensive enough)

    With this in mind, i produced the following script:

    function Loader( $Class ) {
        // Cut Root-Namespace
        $Class = str_replace( __NAMESPACE__.'\\', '', $Class );
        // Correct DIRECTORY_SEPARATOR
        $Class = str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, __DIR__.DIRECTORY_SEPARATOR.$Class.'.php' );
        // Get file real path
        if( false === ( $Class = realpath( $Class ) ) ) {
            // File not found
            return false;
        } else {
            require_once( $Class );
            return true;
        }
    }
    

    Where to place it..

    • /Loader.php <-- there goes the loader
    • /Controller/... <-- put ur stuff here
    • /Model/... <-- or here, etc
    • /...

    Remeber:

    • if you use a root namespace, the loader has to be in this namespace too
    • you may prefix $Class to match your needs (controller_base {} -> class_controller_base.php)
    • you may change __DIR__ to an absolute path containing your class files (e.g. "/var/www/classes")
    • if you don't use namespaces, all files has to be in the same directory together with the loader (bad!)

    Happy coding ;-)


    A little review at other answers: THIS IS JUST MY PERSONAL OPINION - NO OFFENSE INTENDED!

    https://stackoverflow.com/a/5280353/626731 @alex good solution, but don't make you class names pay for bad file structures ;-) this is job for namespaces

    https://stackoverflow.com/a/5280510/626731 @Mark-Eirich it works, but its pretty nasty/ugly/slow/stiff[..] style to do it this way..

    https://stackoverflow.com/a/5284095/626731 @tealou for his problem to be solved this is the most clear approach so far :-) ..

    https://stackoverflow.com/a/9628060/626731 @br3nt this reflects my point of view, but please(!) .. dont use strtr!! .. which brings me to:

    https://stackoverflow.com/a/11866307/626731 @Iscariot .. to you, a little "you-know-bullshit-benchmark:

    Time        sprintf preg_replace strtr    str_replace v1 str_replace v2
    08:00:00 AM 1.1334  2.0955       48.1423  1.2109         1.4819
    08:40:00 AM 1.0436  2.0326       64.3492  1.7948         2.2337
    11:30:00 AM 1.1841  2.5524       62.0114  1.5931         1.9200
    02:00:00 PM 0.9783  2.4832       52.6339  1.3966         1.4845
    03:00:00 PM 1.0463  2.6164       52.7829  1.1828         1.4981
    Average     1.0771  2.3560       55.9839  1.4357         1.7237
    
    
    Method         Times Slower (than sprintf)
    preg_replace   2.19
    strtr          51.97
    str_replace v1 1.33
    str_replace v2 1.6
    

    Source: http://www.simplemachines.org/community/index.php?topic=175031.0

    Questions?.. (But he is in fact right about full path including)

    https://stackoverflow.com/a/12548558/626731 @Sunil-Kartikey https://stackoverflow.com/a/17286804/626731 @jurrien

    NEVER loop in time critical environment! Don't search for files on os! - SLOW

    https://stackoverflow.com/a/21221590/626731 @sagits .. much better than Marks ;-)

提交回复
热议问题