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

Review: recursive file loading program - 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

In case you still haven't had opportunity to read the last article of the series, where I explained how to build a file loading class with recursive search capabilities, below I've included the class's full source code. Pay close attention to its definition, please:

 

<?php

// define a recursive loader class

class Loader

{

private $file = '';

private $path = '';

// constructor (not implemented)

public function __construct(){}

 

// set file to load

public function set_file($file)

{

$this->file = $file;

}

 

// get file to load

public function get_file()

{

return $this->file;

 }

 

// set path to load file

public function set_path($path)

{

$this->path = $path;

}

 

// get path to load file

public function get_path()

{

return $this->path;

}

// load recursively specified file

public function load($file, $path)

{

if (file_exists($file))

{

require_once($file);

return;

}

else

{

if (is_dir($path))

{

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

{

// search recursively the specified file

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

{

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

{

$path .= '/' . $dir;

$file = $path . '/' . $this->file;

$this->load($file, $path);

}

}

}

closedir($handle);

}

}

}

}

 

If you look closely at the above "Loader" class, then you'll realize that apart from its setters and getters, its actual workhorse is its "load()" method. This method is not only is responsible for including a specified file via the "require_once()" PHP function, but performs this task by using a simple recursive algorithm that traverses the proper directories on the web server. 

So far, so good. Having quickly reviewed the definition of the file loader class, below I included a couple of examples that show how to include two basic PHP files. That being said, here are the two files to be included:

 

('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 here are two examples that demonstrate a concrete usage of the previous "Loader" class, using in each case a different path to look for the targeted files. Here's the first example:

 

// create instance of Loader class

$loader = new Loader();

// set file to load

$loader->set_file('sample_file1.php');

// set path of specified file

$loader->set_path($_SERVER['DOCUMENT_ROOT'] . '/folder1);

// try to load specified file

$loader->load($loader->get_file(), $loader->get_path());

// set another file to load

$loader->set_file('sample_file2.php');

// try to load specified file

$loader->load($loader->get_file(), $loader->get_path());

 

/* displays the following

This file has been loaded with the Loader class.

This file has been loaded at the following time 10:32:51

*/

 

Finally, here's the second example, which utilizes a distinct starting path to look for the specified files:

 

// create instance of Loader class

$loader = new Loader();

// set file to load

$loader->set_file('sample_file1.php');

// set path of specified file

$loader->set_path($_SERVER['DOCUMENT_ROOT']);

// try to load specified file

$loader->load($loader->get_file(), $loader->get_path());

// set another file to load

$loader->set_file('sample_file2.php');

// try to load specified file

$loader->load($loader->get_file(), $loader->get_path());

 

/* displays the following

This file has been loaded with the Loader class.

This file has been loaded at the following time 10:45:02

*/

 

Well, I  think that the above code samples should give you a clear idea of how the file loader class does its thing. However, as I mentioned in the introduction, it's necessary to spawn an instance of the class to include a targeted file, which in truth can be completely avoided simply by declaring its "load()" method static. 

So, based on this concept, in the section to come I'm going to improve the definition of the "Loader()" class by modifying the method discussed before, and incorporating into it a basic error handling mechanism as well. 

Now, click on the link below and proceed to read the following segment.



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