Home arrow PHP arrow Page 3 - Working Out of the Object Context to Build Loader Apps in PHP

Improving the definition of the Loader class - PHP

Welcome to the fifth part of an eight-part article series that teaches you how to build loader applications with PHP. In this installment of the series, I explain how to create a small, efficient file loader class, with a difference: no instance of it needs to be spawned to include a targeted file, thanks to the implementation of a static recursive loading method.

TABLE OF CONTENTS:
  1. Working Out of the Object Context to Build Loader Apps in PHP
  2. Review: recursive file loading program
  3. Improving the definition of the Loader class
  4. The Loader class in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
June 25, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

As I explained in the segment that you just read, it would be really useful to declare the "load()" method of the previous file loading class static, to make it callable only out of the object's context. To do so, I'm simply going to refine the signature of the class in question, which now will look as follows:

 

class Loader

{

// constructor (not implemented)

public function __construct(){}

 

// load recursively a specified file

public static function load($file, $path)

{

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

if (file_exists($filepath))

{

require_once($filepath);

}

else

{

if (!is_dir($path))

{

throw new Exception('The supplied path is invalid!.');

}

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

{

// search recursively the specified file

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

{

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

{

$path .= '/' . $dir;

self::load($file, $path);

}

}

closedir($handle);

}

}

throw new Exception('The specified file was not found!.');

}

}

 

As you can see, not only have setters and getters been removed from the "Loader" class, but as I mentioned before, its "load()" method has been declared static for the reasons given previously. Besides, it's valid to notice another subtle enhancement that has been also introduced in this method: it now will throw a regular exception if the file to be included isn't found across the supplied path. 

Definitely, the loader class is now a bit more functional and, best of all, it can be used for including files dynamically without having to deal with an instance of it. So, what's the next step to take? Well, if you're like me, then you'll surely want to see it in action, right? 

Therefore, in the final section of this article I'm going to develop another example, which will be aimed at demonstrating how to use the loader class out of the object scope. 

To learn the full details of how this concluding example will be created, click on the link below and read the next few lines.



 
 
>>> 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: