The Autoload Magic Function in PHP 5

Among the improvements and new features that were introduced to PHP 5, there’s a set of special functions, popularly known as magic functions. These allow you to perform all sorts of smart tasks, ranging from overloading properties and methods in classes, to using destructors and triggering automatically predefined processes when serializing and unserializing objects. This is the conclusion to a seven-part series that shows you how to use the magic functions in PHP 5.

So, if you’re a PHP developer who wants to learn how to put these magic functions to work for you in a truly effortless way, or wishes to fill some gaps in your knowledge regarding their correct implementation and usage, then this group of tutorials might be the guide that you’re looking for.

And now that you know what to expect from this series of articles, it’s to review the topics that were discussed in the last one. In that particular tutorial I discussed the implementation of a destructor method within a simple class, via the pertinent “__destruct()” function.

In that particular situation, this function was provided with the capability to save an instance of the originating class to a session variable before being destroyed by the PHP interpreter. In doing so, I hopefully demonstrated how to create basic persistent objects by using only a simple destructor. Not too bad at all, right?

However, PHP 5 offers yet another magic method that can be extremely useful for loading classes automatically, without having to use explicitly any “include()/include_once()” or “require()/require_once()” function. As the article’s title suggests, I’m talking specifically about the “__autoload()” function, which deserves a deeper analysis.

Therefore, this last chapter of the series will be focused exclusively on exploring the potential of this function for including classes in a transparent way. So, jump ahead and start learning more about it!

{mospagebreak title=Review: the destruct method}

In case you haven’t had the chance to read the preceding article of this series, where I explained how to implement a simple destructor inside a class to create objects that are capable of persisting across several HTTP requests, below I reintroduced the full source code of this class, accompanied by a short example that shows how to use it.

Here’s the corresponding code sample:

// define ‘User’ class (implements the __destruct() magic method)

class User

{

// constructor (not implemented)

public function _construct(){}

 

// set undeclared property in a restrictive way

public function __set($property, $value)

{

if (in_array($property, array(‘fname’, ‘lname’, ‘email’)) === TRUE)

{

$this->$property = $value;

}

}

 

// get declared property

public function __get($property)

{

if (isset($this->$property))

{

return $this->$property;

}

}

 

// single point to fetch user data

public function __call($method, $args)

{

if ($method === ‘fetch’ AND empty($args) === FALSE)

{

return $this->$args[0];

}

}

public function __destruct()

{

session_start();

$_SESSION['user'] = serialize($this);

}

}

Apart from overloading properties and methods via the “__set()”, “__get()” and “__call()” functions, the above class also gives a simple implementation for its destructor, which saves an instance of any user object to a session variable right before being destroyed by the PHP interpreter.

Of course, the previous code sample would be incomplete if I don’t create a script that shows how the destructor does its business. Here it is:

// example on implementing the ‘__destruct()’ magic method

$user = new User();

$user->fname = ‘Alejandro';

$user->lname = ‘Gervasio';

$user->email = ‘alejandro@mydomain.com';

Though it’s a little bit rudimentary, this example depicts pretty clearly how to use the “__destruct()” method to create classes that can save themselves to a predefined storage mechanism, in this case a session variable. However, it’s possible to use a database or even a flat file to produce a similar result.

Now that you’re pretty familiar with implementing destructors, it’s time to explore another magic function provided by PHP 5. So, in accordance with the concepts I expressed at the beginning, the last function that I’m going to cover in this series will be the popular “__autoload(),” which can be really useful for loading classes without having to code any PHP include statements explicitly.

The implementation and use of this function will be discussed in detail in the following section. Thus, to learn more, please click on the link shown below and keep reading.

{mospagebreak title=Loading classes automatically with the autoload magic function}

Without a doubt, one of the most useful PHP 5 functions in the “magic” category is the handy “__autoload().” As you may have guessed, when properly implemented, this function permits you to load automatically classes whose definitions haven’t been explicitly included into a given script.

This ability is indeed remarkable (despite the disparate opinions of other developers), since it’s possible to build, for instance, bootstrap files that load classes in a transparent manner without having to directly code lots of PHP includes. Of course, the “__autoload()” function isn’t the only one that can be used for including classes automatically; there are others that are even more powerful. One such example is the “spl_autoload_register()” function provided by the Standard PHP Library, which allows you to register custom functions or methods for performing this process in a more thorough way.

However, for now I’ll keep this tutorial focused on the advantages to using the “__autoload()” function only. So, with that goal in mind, what I’m going to do next will consist of defining another example class, which will be “autoloaded()” by a script which will be coded afterwards.

That being explained, here’s the full source of the aforementioned sample class, called “User” once again. Check it out:

class User

{

private $name = ‘Alejandro';

private $email = ‘alejandro@domain.com';

 

// constructor

public function __construct($name = ”, $email = ”)

{

if ($name != ”)

{

$this->name = $name;

}

if ($email != ”)

{

$this->email = $email;

}

}

 

// get user’s name

public function get_name()

{

return $this->name;

}

 

// get user’s email address

public function get_email()

{

return $this->email;

}

 

function __toString()

{

return ‘Name: ‘ . $this->name . ‘ Email: ‘ . $this->email;

}

}

 

As you can see, the previous “User” class has nothing special; it’s useful only for storing the full name and email address of a user and retrieving the values assigned to these properties via its “__toString()” method. This method is also a magic function, but for the sake of brevity it won’t be covered in this article series.

So far, everything looks good, since at this point there’s a class available for testing the real functionality of the “__autoload()” function. But wait a minute! What functionality am I’m talking about if the function in question hasn’t been implemented yet? Well, don’t worry, because in the next section I’m going to do that, and I’m also going to demonstrate how to automatically load the previous “User” class within a simple script.

As I just mentioned, all of these tasks will be performed in the following segment, so to get there, click on the link that appears below and keep reading.

{mospagebreak title=Loading classes in the background with the autoload function}

In the section you just read, I defined a class called “User.” Admittedly, it doesn’t perform any particularly useful tasks, but it’s handy for demonstrating how it can be loaded in the background by the “__autoload()” function.

To accomplish this however, it’s necessary to give this function a concrete implementation, which will be very easy to understand. Take a look at the following example, which shows how to turn the “__autoload()” function into a more useful piece of code:  

function __autoload($class)

{

$file = $class . ‘.php';

if(!file_exists($file))

{

eval("class $class {}");

throw new Exception(‘Class ‘ . $class . ‘ not found.’);

}

require_once($file);

}

There you have it. As you can see, all that I did before was define the “__autoload()” function so it can load in the background the file corresponding to a class whose signature hasn’t been found by the PHP interpreter.

If the referenced class isn’t found, then an exception will be thrown, and eventually it will be caught either by a “catch()” block or by a custom exception handler. Logically, in the last instance a “require_once()” function is called within “__autoload(),” but it must be coded only once. Otherwise, in a typical scenario, one of these should have been coded per each requested file.

Now that you’ve surely grasped the underlying logic of the previous “__autoload()” function, and assuming that the following script and the file that contains the “User” class resides in the same location on the file system, loading the class in the background would be as simple as this:

// create instance of User class

$user = new User();

// display user data

echo $user;

Short to write and read, isn’t it? In this case the script creates a new instance of the “User” class and displays the values assigned to its properties. This happens with only one single class. However, where the “__autoload()” function really shines is when it comes to including multiple classes within the same script, since it lets you get rid of the annoyance of this process.

And with this final example, I’m finishing this round up of the most important magic functions that come included with PHP 5. If you haven’t used them yet when developing your own web application, I encourage you to give them a try, particularly for overloading properties and methods in a straightforward –- yet careful — way. You won’t be disappointed, trust me!

Final thoughts

Nothing lasts forever, and this series isn’t an exception. Nonetheless, in the end I hope the experience has been instructive. In this round up you learned how to implement and use the most relevant magic functions provided by PHP 5.

As with other features of the language, don’t abuse these functions, particularly when overloading methods and properties of a class. This can lead to making your code less readable and harder to maintain for other developers.

Don’t miss the next PHP development tutorial!

[gp-comments width="770" linklove="off" ]

chat