Home arrow PHP arrow Page 2 - Developing a Recursive Loading Class for Loader Applications in PHP

Review: building a basic file loading class with PHP 5 - PHP

Welcome to the third part of an eight-part series on building loader applications in PHP. In the tutorials that comprise this series you’ll find numerous examples aimed at demonstrating how to create small, yet efficient, resource loader classes, not only by taking advantage of PHP includes, but the handy “__autoload()” magic function and the Standard PHP Library (SPL) as well.

TABLE OF CONTENTS:
  1. Developing a Recursive Loading Class for Loader Applications in PHP
  2. Review: building a basic file loading class with PHP 5
  3. Defining a class with recursive file searching capabilities
  4. Defining the recursive load() method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
June 18, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

At this point, I'm sure you want to learn how to build a file loading class with recursive searching capabilities. However, before we do that, it would be helpful to quickly recall the definition and usage of the “Loader” class developed in the preceding part of the series, so you can compare it with the one that I plan to create. 

Below I listed the full source code of the previous loader class, accompanied by a short example that shows how to work with it. First, here’s the signature of the “Loader” class:

 

// define the loader class (the 'load()' method is declared static)

class Loader

{

// constructor (not implemented)

public function __construct(){}

 

// load specified file

public static function load($filepath)

{

if (!file_exists($filepath))

{

throw new Exception('The specified file cannot be found!');

}

require_once($filepath);

}

}

 

Next, we'll reintroduce the two sample files that will be included by the class. Here they are:

 

('sample_file1.php')

 

<?php

echo ' This file has been loaded with the Loader class.' . '<br />';

?>

 

 

('sample_file2.php')

 

<?php

echo 'This file has been loaded at the following time: ' . date('H:i:s');

?>

 

And finally, here’s a small code snippet that shows the “Loader” class in action, using its static “load()” method:

 

<?php

try

{

// call 'load()' method statically and load specified files

Loader::load('sample_file1.php');

Loader::load('sample_file2.php');

 

/* displays the following

This file has been loaded with the Loader class.

This file has been loaded at the following time 11:37:09

*/

}

catch (Exception $e)

{

echo $e->getMessage();

exit();

}

?>

 

From the code sample shown above, it’s clear to see how simple the logic is that is implemented by the pertinent “Loader()” class. All it does is include a specified file via its static “load()” method, which is merely a proxy for the native “require_once()” PHP function. 

In general terms, due to its basic structure, this class could be considered a file loader helper, rather than a core library of a fictional framework. Of course, as you might have imagined, it’s perfectly feasible to use the skeleton of the class and construct a more advanced and complex version of it, capable of searching recursively targeted files and including them inside a PHP application. 

That’s exactly what I’m going to do in the course of the following section: build a recursive file loader class. So, if you want to learn the full details of this process, click on the link that appears below and keep reading.



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