Home arrow PHP arrow Page 4 - Using the spl_autoload() Functions to Build Loader Apps in PHP

Introducing the spl autoload register function - PHP

Welcome to the seventh installment of an eight-part series on building loader applications in PHP. In this part, you will learn how to use the “spl_autoload(),” “spl_register()” and “spl_register()” functions to build a small file loader class. This class will be able to perform recursive searches through the file system to find a targeted resource.

TABLE OF CONTENTS:
  1. Using the spl_autoload() Functions to Build Loader Apps in PHP
  2. Review: the autoload magic function
  3. Using the spl autoload extensions and spl autoload functions
  4. Introducing the spl autoload register function
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
July 01, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

As I said in the segment that you just read, the Standard PHP Library comes bundled with a handy function called “spl_autoload_register(),” which allows you to use a callback method or function for automatically loading classes.

To illustrate how to use this function in more detail, below I defined another sample class called “Autoloader(),” whose “autoload()” method will be tied to the function in question.

That being explained, here’s how to use this class in a concrete case:

// example on using the spl_autoload_register() function

// register autoload() method of Autoloader class

 

spl_autoload_register('Autoloader::autoload');

 

// define Autoloader class

class Autoloader

{

public function __construct(){}

 

// autoload specified file

public static function autoload($file)

{

// the path should be defined as a constant or similar

 $path = $_SERVER['DOCUMENT_ROOT'] . '/loader_classes/';

$filepath = $_SERVER['DOCUMENT_ROOT'] . '/loader_classes/' . $file . '.php';

if (file_exists($filepath))

{

require_once($filepath);

}

else

{

Autoloader::recursive_autoload($file, $path);

}

}

 

// try to load recursively the specified file

public static function recursive_autoload($file, $path)

{

if (FALSE !== ($handle = opendir($path)))

{

// search recursively the specified file

while (FAlSE !== ($dir = readdir($handle)))

{

if (strpos($dir, '.') === FALSE)

{

$path .= '/' . $dir;

$filepath = $path . '/' . $file . '.php';

if (file_exists($filepath))

{

require_once($filepath);

break;

}

Autoloader::recursive_autoload($file, $path);

}

}

closedir($handle);

}

}

}

// create instance of User class (it's autoloaded by the Autoloader class)

$user = new User();

// display user data

echo $user;

There you have it. As you can see, the “autoload()” method defined within the above class has been linked to the “spl_autoload_register()” function, meaning that this method will be called by the PHP parser when attempting to include a specific class.

Also, the method is capable of performing recursive searches to find the class that needs to be included, as you can see clearly in the prior example. Not too hard to grasp, right?

Finally, with this wrapping code sample I’m finishing this seventh part of the series. As always, feel free to edit and improve all of the examples shown in this article. This way you can arm yourself with a better background in building class loading programs in PHP 5. The experience will be extremely educational, believe me.

Final thoughts

In this seventh installment of the series, you learned how to use the “spl_autoload(),” “spl_register()” and “spl_register()” functions to build an small file loader class which was capable of performing recursive searches through the file system to find a targeted resource.

In this particular case, the recursive algorithm used by the class was rather lengthy and hard to grasp. However, the Standard PHP library includes a set of iterators that allow you to traverse directories in a truly effortless fashion. So, in the last chapter of this series I’m going to incorporate an iterator into the previous loader class, making its source code a bit more compact and readable.

Don’t miss the final tutorial!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: