PHP
  Home arrow PHP arrow Page 2 - Working Out of the Object Context to Build Loader Apps 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

Working Out of the Object Context to Build Loader Apps in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2009-06-25


    Table of Contents:
  • Working Out of the Object Context to Build Loader Apps in PHP
  • Review: recursive file loading program
  • Improving the definition of the Loader class
  • The Loader class in action

  • 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


    Working Out of the Object Context to Build Loader Apps in PHP - Review: recursive file loading program
    ( Page 2 of 4 )

    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
     

       

    PHP ARTICLES

    - 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...
    - Mastering WHILE Loops for PHP and MySQL





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