Calipus Software

What is spl_autoload_register, spl_autoload and autoload

PHP does have some awesome function which can be very handy. SPL register function is one of them, we are going to talk about.

As PHP manual says

spl_autoload_register — Register given function as __autoload() implementation

So what exactly it means ? Let’s try to understand with the help of an example.

[code]

function Autoloader($className)
{
require_once $path.$className.’.php';
}

spl_autoload_register(‘Autoloader’);

$myObj = new MyClass();
[/code]

Here we have just instantiated a class named as “MyClass” with out specifying include or require statements.

Is not it cool ? So how have we done it ? Let’s dig in.

Here we have registered a function named as “Autoloader” with spl_autoload_register() function. When, we instantiate “MyClass”, class name(MyClass) is passed by PHP to “spl_autoload_register()”, where registered function picks it up as a parameter. And in defination of Autoloader function, we have included the path of that class file.

This is simply great!! Just think of a situation, where you are having couple of classes and all of them are not going to be used in each case OR on each page. In such cases, spl_autoload_register() can be a real saviour for you.

You just need to instantiate and spl_autoload_register() will take care of rest.

Instead of registering a function you can also do it as follow:

[code]
spl_autoload_register(function ($class) {
include ‘classes/’ . $class . ‘.class.php';
});
[/code]

But this chunk of code works only with PHP >= 5.3.0

You can consider spl_autoload_register() as a replacement of __autoload(), which can be defined as:

__autoload() — Attempt to load undefined class

Let’s try to understand with the help of an example.

[code]
<?php
// we’ve writen this code where we need
function __autoload($classname) {
$filename = "./". $classname .".php";
include_once($filename);
}

// we’ve called a class ***
$obj = new myClass();
?>
[/code]

But it has a demerit that you need to write this function in each of your class file.

[code]I shall strongly suggest that use spl_autoload_register() instead __autoload() [/code]

Now let’s try to understand about spl_autoload()

PHP Manual says-

This function is intended to be used as a default implementation for __autoload(). If nothing else is specified and spl_autoload_register() is called without any parameters then this function will be used for any later call to __autoload().

If all of your class files are placed in a single directory then you can set the path of same using “set_include_path()” function and can let PHP SPL function know that what extension to look for, using “spl_autoload_extensions()”. And finally call spl_autoload_register() to register the path and extension set in above two lines.

[code]
set_include_path(get_include_path() . PATH_SEPARATOR . ‘/path/to/my/classes/’);
spl_autoload_extensions(‘.php, .inc’);
spl_autoload_register();
[/code]

Hope it helps!

Sandeep Kumar

Sandeep Kumar is a web consultant having experience of more than 9 years in website development, Brand Promotion, Digital Marketing and Social Media Marketer. Sandeep consults, develops, speak and write about building high quality web based solutions. He loves to interact with people and believe that communication is the key to success and helps to solve complex problems. He can be reached at sandeep@calipus.com

Leave a Reply