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

Defining a class with recursive file searching capabilities - 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

As I stated in the previous segment, it’s possible to build a file loading class that has the ability to look for a targeted file recursively. To create a class with this capability, first I’m going to define its basic structure along with a few setter and getter methods, which will be responsible for setting and getting the file for which to search, and for performing the same tasks with the starting file path. 

If this brief description sounds a bit confusing to you, the following code sample should help to dissipate your doubts. Take a look at it:

 

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

}

}

 

As you can see, the above “Loader()” class has some simple methods, apart from the constructor, for setting and getting the file that needs to be included, and for establishing and retrieving the path from which to start searching the file. 

These setters and getters are very easy to understand, so I’m not going to spend more time discussing how they work. But wait a minute! Didn’t I say at the beginning of this section that my purpose here was building a file loader class that is capable of including a specified file through a recursive search? Yes, I did. 

But this functionality needs to be implemented by means of a separate method, which not surprisingly will be called “load().” 

Therefore, to see how this whole new method will be properly defined, read the next section.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap

Dev Shed Tutorial Topics: