PHP
  Home arrow PHP arrow Page 4 - Developing a Recursive Loading Class for Loader Applications in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Developing a Recursive Loading Class for Loader Applications in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2009-06-18


    Table of Contents:
  • Developing a Recursive Loading Class for Loader Applications in PHP
  • Review: building a basic file loading class with PHP 5
  • Defining a class with recursive file searching capabilities
  • Defining the recursive load() method

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Developing a Recursive Loading Class for Loader Applications in PHP - Defining the recursive load() method
    ( Page 4 of 4 )

    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
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek