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

Defining the recursive load() method - 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

In reality, building a method that recursively traverses the file system and includes a specified file doesn’t differ too much from defining other common recursive search methods that you’ve probably coded many times before. 

This concept will be better understood if you look at the “load()” method below, which performs the aforementioned recursive search in a straightforward manner:

 

// 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);

}

}

}

 

As usual, coding recursive methods and functions is a pretty tricky process, and the “load()” method isn’t an exception. However, its flow of execution is quite simple to follow. If the targeted file is found, then it’s included directly via the “require_once()” function. Otherwise, a recursive search is performed through the web server’s file system, starting from the specified path. 

Having explained how the “load()” method functions, it’s time to show the full source code of the “Loader()” class, this time including the signature of the method in question. Here it is:

 

<?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);

}

}

}

}

 

So far, so good, right? At this stage, you've learned how to build a loader class that’s capable of recursively searching a determined file based on a supplied path and including it if it’s found. 

Logically, the last thing that remains undone is developing a example application that shows how to use the class in a concrete case. But this will be done in the next article of the series. Meanwhile, feel free to tweak the “Loader” class’ source code, and try to introduce into it your own enhancements.

Final thoughts  

That’s all for now. In this third installment of the series, I discussed the progressive development a file loading class, which had the ability to include a specified file through a recursive search. 

Naturally, the class can be improved, particularly when it comes to refactoring its “load()” core method, but this topic will be covered in more detail in upcoming articles of the series. 

In the forthcoming tutorial, I’m going to set up some illustrative examples, so you’ll be able to see how the previous “Loader” class does its business. 

Don’t miss the next part!



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