PHP Tutorials : Autoload PHP Classes
This tutorials post will explain PHP autoloader and namespace concept using few basic question and answer regarding PHP autoload functionality along with some examples.
What is Autoloading in PHP?PHP provides helper functions to include or import PHP files, if it has not been included before. if we use PHP autoload then we don't need to write require or include statement for each PHP class files.
Why we need auto loading?It is a good question. why we need it, although we can write require or include statements whenever we need. As PHP has evolved using OOPs concept, we create more classes for our PHP project and create a separate PHP file for each single PHP class. Hence we increase the number of require or include statements. therefore we need some standard way to include each files rather specifying include statement by our self every time. that is why PHP has provided some useful methods to resolve this problem.
PHP Autoloading MethodsPHP uses two function, one is magic method __autoload and another is spl_autoload_register. each of these have their own benefit. they accept one parameter which turns into class name whenever we instantiate that class object. inside these method we could write our logic to load PHP class file.
What is difference between __autoload and sp_autload_resgister function?__autoload method is magic method which means you can define it only once. therefore you would have only one logic to include PHP class files whereas spl_autoload_register is defined function which accept function name as parameter and we can define specified function with our logic. that mean we can have multiple implementation of inclusion of PHP files with help of spl_autoload_register.
What is namespace?PHP normally wont allow two class with same name. but later 5.3 introduce concept of namespace which create a ownership of class so that two different class can have same class name but with different ownership.
What is importance of namespace in autloadingwhen we autoload class we get a parameter into autload function. when you echo that parameter it will print that class name and namespace provides directory structure for classes.
if we autoload above class then we will get whole namespace of that class along with class name e.g namespace_autoload.php
with help of this information we can organize classes according to our project directory structure. let say you have following project structure
then you can create PHP classes with namespace as
as you can see how namespace helps to organize project structure and autoload the classes.
What is difference between following two sets of statementsnamespace autloading Direct include files
General difference is require statement accept full directory path where in case of namespace we can set logic to autoload PHP files with different directory structure and can minimize the namespace length. let see the example, suppose you have following directory for your project library files
using require statement need to write following code.
which is very lengthy and not properly organized with help of autoload and namespace we can reduce and class path and organize structure in much proper ways
- autoload helps to load all PHP classes of a PHP project.
- namespace helps to organize your PHP classes
- combination of autoload and namespace organize and load PHP project in standardized way.