Advanced PHP tutorial 9: Autoload your classes PSR-0

In PHP the most common way to load a class from a different file is to require_once or include_once. The problem with this approach is that you need to manually add each file. What if a file is not used? You still added it, which takes up time and memory. What if the order of your includes change? More manual work for you…

The solution is to use composer and its handy autoloader feature. You can autoload classes and namespaces using the PSR-0 or PSR-4 standards, and structure your modules in a way similar to how Java does it (directory structure matches namespace/package structure).

The result is simple, easy, and elegant: at the top of your PHP scripts/application, simply include the vendor/autoload.php file, and anything you reference at run time is lazily loaded into memory, and subsequent objects are then loaded and instantiated.

Any time you add/remove a namespace to your modules, re-run the command “composer install” or “composer update” so that the autoload files are created again.

Programming tutorials by Easy Learn Tutorial – because anyone can learn how to become an expert software and web developer!

Copyright (c) 2013 Rodrigo Silveira –



Do NOT follow this link or you will be banned from the site!