PHP
  Home arrow PHP arrow Page 3 - 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? 
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 - Improving the definition of the Loader class
    ( Page 3 of 4 )

     

    As I explained in the segment that you just read, it would be really useful to declare the "load()" method of the previous file loading class static, to make it callable only out of the object's context. To do so, I'm simply going to refine the signature of the class in question, which now will look as follows:

     

    class Loader

    {

    // constructor (not implemented)

    public function __construct(){}

     

    // load recursively a specified file

    public static function load($file, $path)

    {

    $filepath = $path . '/' . $file;

    if (file_exists($filepath))

    {

    require_once($filepath);

    }

    else

    {

    if (!is_dir($path))

    {

    throw new Exception('The supplied path is invalid!.');

    }

    if (FALSE !== ($handle = opendir($path)))

    {

    // search recursively the specified file

    while (FAlSE !== ($dir = readdir($handle)))

    {

    if (strpos($dir, '.') === FALSE)

    {

    $path .= '/' . $dir;

    self::load($file, $path);

    }

    }

    closedir($handle);

    }

    }

    throw new Exception('The specified file was not found!.');

    }

    }

     

    As you can see, not only have setters and getters been removed from the "Loader" class, but as I mentioned before, its "load()" method has been declared static for the reasons given previously. Besides, it's valid to notice another subtle enhancement that has been also introduced in this method: it now will throw a regular exception if the file to be included isn't found across the supplied path. 

    Definitely, the loader class is now a bit more functional and, best of all, it can be used for including files dynamically without having to deal with an instance of it. So, what's the next step to take? Well, if you're like me, then you'll surely want to see it in action, right? 

    Therefore, in the final section of this article I'm going to develop another example, which will be aimed at demonstrating how to use the loader class out of the object scope. 

    To learn the full details of how this concluding example will be created, click on the link below and read the next few lines.



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT