Home arrow PHP arrow Page 2 - Including Files Recursively with Loader Applications in PHP

Review: the loader() class so far - PHP

Welcome to the fourth chapter of the series that shows you how to build loader applications with PHP. Made up of seven parts, this series uses a variety of code samples to teach you how to create modular programs. These programs are capable of recursively including files required by a given application, without having to explicitly call any “include()/include_once()” or “require()/require_once()” PHP function.

TABLE OF CONTENTS:
  1. Including Files Recursively with Loader Applications in PHP
  2. Review: the loader() class so far
  3. Recursively including PHP files
  4. Changing the location of required files
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
June 11, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

Before I start creating some examples aimed at illustrating how to use the file loading class built in the last article of this series, let's recall quickly how it was defined originally. 

So, as a reminder, here’s the full source that corresponds to that class. Pay close attention to it, 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);

}

}

}

}

 

Undeniably, aside from the non-implemented constructor, the workhorse of the above “Loader” class is clearly its “load()” method. It actually takes care of loading a targeted file via the “require()” PHP function. Of course, the most important detail to stress here is the method’s recursive implementation, which permits us to seek the required file through folders and subfolders within the specified path. 

Now that you’re hopefully familiar again with the way that the “Loader()” class functions, it’s time to begin coding some examples that will demonstrate its functionality in concrete situations. 

With that premise in mind, in the following section I’m going to set up a fictional scenario in which the loading class will be used for including a sample file, regardless of its location within a selected directory on the web server. 

To learn the full details of how this brand new example will be developed, 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: