[{"data":1,"prerenderedAt":598},["ShallowReactive",2],{"article:\u002Farticles\u002Fpsr4-autoloader":3,"article-surround:\u002Farticles\u002Fpsr4-autoloader":589},{"id":4,"title":5,"body":6,"date":577,"description":578,"draft":579,"extension":580,"meta":581,"navigation":164,"path":582,"seo":583,"stem":584,"tags":585,"__hash__":588},"articles\u002Farticles\u002Fpsr4-autoloader.md","PSR-4 - Autoloader",{"type":7,"value":8,"toc":575},"minimark",[9,26,33,36,49,52,552,555,571],[10,11,12,13,17,18,21,22,25],"p",{},"Autoloading is a commonly used mechanism that automatically loads classes and\u002For files into memory when they are needed, without having to explicitly include them within each file. In the context of PHP, Composer uses this technique instead of typically using ",[14,15,16],"code",{},"require"," or ",[14,19,20],{},"include"," statements to manually load each class before the class is used. Composer can generate a list of mappings defined in ",[14,23,24],{},"composer.json"," and autoload third party dependencies without much requirement from the user.",[10,27,28,29,32],{},"Outside of Composer, it is possible to write your own autoloader function so that PHP automatically calls classes that haven't been loaded yet. The ",[14,30,31],{},"spl_autoload_register()"," function allows you to register one or more autoloaders.",[10,34,35],{},"Some of the benefits of autoloading are:",[37,38,39,43,46],"ul",{},[40,41,42],"li",{},"Simplicity",[40,44,45],{},"Efficiency",[40,47,48],{},"Maintainability",[10,50,51],{},"Although writing your own autoloader can be fun and is a great learning experience, it is always better to use tried and tested methods such as Composer's autoloader when building applications that may go into production.",[53,54,59],"pre",{"className":55,"code":56,"language":57,"meta":58,"style":58},"language-php shiki shiki-themes github-dark github-light","\u002F\u002F autoloader.php\nclass Autoloader\n{\n    \u002F**\n     * Registers the autoloader function with PHP.\n     *\u002F\n    public static function register()\n    {\n        spl_autoload_register([self::class, 'autoload']);\n    }\n\n    \u002F**\n     * Autoloads the given class.\n     *\n     * @param string $class The fully-qualified class name.\n     * @return bool True if the file was successfully loaded, false otherwise.\n     *\u002F\n    private static function autoload($class)\n    {\n        \u002F\u002F Convert namespace to directory structure and append '.php'\n        $file = str_replace('\\\\', DIRECTORY_SEPARATOR, $class) . '.php';\n\n        \u002F\u002F Check if the file exists and include it if it does\n        if (file_exists($file)) {\n            require $file;\n\n            return true;\n        }\n\n        \u002F\u002F Return false if the class file does not exist\n        return false;\n    }\n}\n\nAutoloader::register();\n\n\u002F\u002F Classes\u002FFoo.php\nnamespace Classes;\n\nclass Foo\n{\n    public function value()\n    {\n        return 'foo';\n    }\n}\n\n\u002F\u002F index.php\nuse Classes\\Foo;\n\ninclude __DIR__ . '\u002FAutoloader.php';\n\n$foo = new Foo();\necho $foo->value();\n","php","",[14,60,61,70,81,88,94,100,106,124,130,153,159,166,171,177,183,198,212,217,233,238,244,284,289,295,310,319,324,335,341,346,352,363,368,374,379,394,399,405,416,421,429,434,446,451,461,466,471,476,482,493,498,514,519,535],{"__ignoreMap":58},[62,63,66],"span",{"class":64,"line":65},"line",1,[62,67,69],{"class":68},"si27w","\u002F\u002F autoloader.php\n",[62,71,73,77],{"class":64,"line":72},2,[62,74,76],{"class":75},"spKkM","class",[62,78,80],{"class":79},"sqoU-"," Autoloader\n",[62,82,84],{"class":64,"line":83},3,[62,85,87],{"class":86},"shWlK","{\n",[62,89,91],{"class":64,"line":90},4,[62,92,93],{"class":68},"    \u002F**\n",[62,95,97],{"class":64,"line":96},5,[62,98,99],{"class":68},"     * Registers the autoloader function with PHP.\n",[62,101,103],{"class":64,"line":102},6,[62,104,105],{"class":68},"     *\u002F\n",[62,107,109,112,115,118,121],{"class":64,"line":108},7,[62,110,111],{"class":75},"    public",[62,113,114],{"class":75}," static",[62,116,117],{"class":75}," function",[62,119,120],{"class":79}," register",[62,122,123],{"class":86},"()\n",[62,125,127],{"class":64,"line":126},8,[62,128,129],{"class":86},"    {\n",[62,131,133,137,140,143,146,150],{"class":64,"line":132},9,[62,134,136],{"class":135},"sTU5a","        spl_autoload_register",[62,138,139],{"class":86},"([",[62,141,142],{"class":75},"self::class",[62,144,145],{"class":86},", ",[62,147,149],{"class":148},"skb7c","'autoload'",[62,151,152],{"class":86},"]);\n",[62,154,156],{"class":64,"line":155},10,[62,157,158],{"class":86},"    }\n",[62,160,162],{"class":64,"line":161},11,[62,163,165],{"emptyLinePlaceholder":164},true,"\n",[62,167,169],{"class":64,"line":168},12,[62,170,93],{"class":68},[62,172,174],{"class":64,"line":173},13,[62,175,176],{"class":68},"     * Autoloads the given class.\n",[62,178,180],{"class":64,"line":179},14,[62,181,182],{"class":68},"     *\n",[62,184,186,189,192,195],{"class":64,"line":185},15,[62,187,188],{"class":68},"     * ",[62,190,191],{"class":75},"@param",[62,193,194],{"class":75}," string",[62,196,197],{"class":68}," $class The fully-qualified class name.\n",[62,199,201,203,206,209],{"class":64,"line":200},16,[62,202,188],{"class":68},[62,204,205],{"class":75},"@return",[62,207,208],{"class":75}," bool",[62,210,211],{"class":68}," True if the file was successfully loaded, false otherwise.\n",[62,213,215],{"class":64,"line":214},17,[62,216,105],{"class":68},[62,218,220,223,225,227,230],{"class":64,"line":219},18,[62,221,222],{"class":75},"    private",[62,224,114],{"class":75},[62,226,117],{"class":75},[62,228,229],{"class":79}," autoload",[62,231,232],{"class":86},"($class)\n",[62,234,236],{"class":64,"line":235},19,[62,237,129],{"class":86},[62,239,241],{"class":64,"line":240},20,[62,242,243],{"class":68},"        \u002F\u002F Convert namespace to directory structure and append '.php'\n",[62,245,247,250,253,256,259,262,265,267,269,272,275,278,281],{"class":64,"line":246},21,[62,248,249],{"class":86},"        $file ",[62,251,252],{"class":75},"=",[62,254,255],{"class":135}," str_replace",[62,257,258],{"class":86},"(",[62,260,261],{"class":148},"'",[62,263,264],{"class":135},"\\\\",[62,266,261],{"class":148},[62,268,145],{"class":86},[62,270,271],{"class":135},"DIRECTORY_SEPARATOR",[62,273,274],{"class":86},", $class) ",[62,276,277],{"class":75},".",[62,279,280],{"class":148}," '.php'",[62,282,283],{"class":86},";\n",[62,285,287],{"class":64,"line":286},22,[62,288,165],{"emptyLinePlaceholder":164},[62,290,292],{"class":64,"line":291},23,[62,293,294],{"class":68},"        \u002F\u002F Check if the file exists and include it if it does\n",[62,296,298,301,304,307],{"class":64,"line":297},24,[62,299,300],{"class":75},"        if",[62,302,303],{"class":86}," (",[62,305,306],{"class":135},"file_exists",[62,308,309],{"class":86},"($file)) {\n",[62,311,313,316],{"class":64,"line":312},25,[62,314,315],{"class":75},"            require",[62,317,318],{"class":86}," $file;\n",[62,320,322],{"class":64,"line":321},26,[62,323,165],{"emptyLinePlaceholder":164},[62,325,327,330,333],{"class":64,"line":326},27,[62,328,329],{"class":75},"            return",[62,331,332],{"class":135}," true",[62,334,283],{"class":86},[62,336,338],{"class":64,"line":337},28,[62,339,340],{"class":86},"        }\n",[62,342,344],{"class":64,"line":343},29,[62,345,165],{"emptyLinePlaceholder":164},[62,347,349],{"class":64,"line":348},30,[62,350,351],{"class":68},"        \u002F\u002F Return false if the class file does not exist\n",[62,353,355,358,361],{"class":64,"line":354},31,[62,356,357],{"class":75},"        return",[62,359,360],{"class":135}," false",[62,362,283],{"class":86},[62,364,366],{"class":64,"line":365},32,[62,367,158],{"class":86},[62,369,371],{"class":64,"line":370},33,[62,372,373],{"class":86},"}\n",[62,375,377],{"class":64,"line":376},34,[62,378,165],{"emptyLinePlaceholder":164},[62,380,382,385,388,391],{"class":64,"line":381},35,[62,383,384],{"class":135},"Autoloader",[62,386,387],{"class":75},"::",[62,389,390],{"class":79},"register",[62,392,393],{"class":86},"();\n",[62,395,397],{"class":64,"line":396},36,[62,398,165],{"emptyLinePlaceholder":164},[62,400,402],{"class":64,"line":401},37,[62,403,404],{"class":68},"\u002F\u002F Classes\u002FFoo.php\n",[62,406,408,411,414],{"class":64,"line":407},38,[62,409,410],{"class":75},"namespace",[62,412,413],{"class":79}," Classes",[62,415,283],{"class":86},[62,417,419],{"class":64,"line":418},39,[62,420,165],{"emptyLinePlaceholder":164},[62,422,424,426],{"class":64,"line":423},40,[62,425,76],{"class":75},[62,427,428],{"class":79}," Foo\n",[62,430,432],{"class":64,"line":431},41,[62,433,87],{"class":86},[62,435,437,439,441,444],{"class":64,"line":436},42,[62,438,111],{"class":75},[62,440,117],{"class":75},[62,442,443],{"class":79}," value",[62,445,123],{"class":86},[62,447,449],{"class":64,"line":448},43,[62,450,129],{"class":86},[62,452,454,456,459],{"class":64,"line":453},44,[62,455,357],{"class":75},[62,457,458],{"class":148}," 'foo'",[62,460,283],{"class":86},[62,462,464],{"class":64,"line":463},45,[62,465,158],{"class":86},[62,467,469],{"class":64,"line":468},46,[62,470,373],{"class":86},[62,472,474],{"class":64,"line":473},47,[62,475,165],{"emptyLinePlaceholder":164},[62,477,479],{"class":64,"line":478},48,[62,480,481],{"class":68},"\u002F\u002F index.php\n",[62,483,485,488,491],{"class":64,"line":484},49,[62,486,487],{"class":75},"use",[62,489,490],{"class":135}," Classes\\Foo",[62,492,283],{"class":86},[62,494,496],{"class":64,"line":495},50,[62,497,165],{"emptyLinePlaceholder":164},[62,499,501,503,506,509,512],{"class":64,"line":500},51,[62,502,20],{"class":75},[62,504,505],{"class":135}," __DIR__",[62,507,508],{"class":75}," .",[62,510,511],{"class":148}," '\u002FAutoloader.php'",[62,513,283],{"class":86},[62,515,517],{"class":64,"line":516},52,[62,518,165],{"emptyLinePlaceholder":164},[62,520,522,525,527,530,533],{"class":64,"line":521},53,[62,523,524],{"class":86},"$foo ",[62,526,252],{"class":75},[62,528,529],{"class":75}," new",[62,531,532],{"class":135}," Foo",[62,534,393],{"class":86},[62,536,538,541,544,547,550],{"class":64,"line":537},54,[62,539,540],{"class":135},"echo",[62,542,543],{"class":86}," $foo",[62,545,546],{"class":75},"->",[62,548,549],{"class":79},"value",[62,551,393],{"class":86},[10,553,554],{},"Resources:",[37,556,557,565],{},[40,558,559],{},[560,561,562],"a",{"href":562,"rel":563},"https:\u002F\u002Fwww.php-fig.org\u002Fpsr\u002Fpsr-4\u002F",[564],"nofollow",[40,566,567],{},[560,568,569],{"href":569,"rel":570},"https:\u002F\u002Fgithub.com\u002Fphp-fig\u002Ffig-standards\u002Fblob\u002Fmaster\u002Faccepted\u002FPSR-4-autoloader-examples.md",[564],[572,573,574],"style",{},"html pre.shiki code .si27w, html code.shiki .si27w{--shiki-default:#6A737D;--shiki-light:#6A737D}html pre.shiki code .spKkM, html code.shiki .spKkM{--shiki-default:#F97583;--shiki-light:#D73A49}html pre.shiki code .sqoU-, html code.shiki .sqoU-{--shiki-default:#B392F0;--shiki-light:#6F42C1}html pre.shiki code .shWlK, html code.shiki .shWlK{--shiki-default:#E1E4E8;--shiki-light:#24292E}html pre.shiki code .sTU5a, html code.shiki .sTU5a{--shiki-default:#79B8FF;--shiki-light:#005CC5}html pre.shiki code .skb7c, html code.shiki .skb7c{--shiki-default:#9ECBFF;--shiki-light:#032F62}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}",{"title":58,"searchDepth":72,"depth":72,"links":576},[],"2024-03-24","How class autoloading actually works in PHP, and how to write a PSR-4 compliant autoloader yourself with spl_autoload_register - and when to just use Composer.",false,"md",{},"\u002Farticles\u002Fpsr4-autoloader",{"title":5,"description":578},"articles\u002Fpsr4-autoloader",[57,586,587],"psr","autoloading","KGlhVK1dEheMqU-vXOVgBU26dbIkIH1f05NQZECnhU0",[590,594],{"title":591,"path":592,"stem":593,"children":-1},"Clean Architecture for PHP Applications","\u002Farticles\u002Fclean-architecture-for-php-applications","articles\u002Fclean-architecture-for-php-applications",{"title":595,"path":596,"stem":597,"children":-1},"Simplify Your Build Process with Makefile","\u002Farticles\u002Fsimplify-your-build-process-with-makefile","articles\u002Fsimplify-your-build-process-with-makefile",1782332463234]