PHP
  Home arrow PHP arrow Page 2 - Using Static Methods 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

Using Static Methods to Build Loader Apps in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 1
    2009-06-03


    Table of Contents:
  • Using Static Methods to Build Loader Apps in PHP
  • Review: a basic file loading system with PHP 5
  • A static file loading method
  • Loading files using a static class 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


    Using Static Methods to Build Loader Apps in PHP - Review: a basic file loading system with PHP 5
    ( Page 2 of 4 )

    In case you still haven’t read the previous chapter of this series, where I went through the development of a pretty basic file loader class in PHP 5, I've included its full signature below, accompanied by an example of its usage, so you can understand how it works.

    Please take a close look at the structure of the sample “Loader” class:

    class Loader

    {

    // constructor (not implemented)

    public function __construct(){}

     

    // load specified file

    public function load($filepath)

    {

    if (!file_exists($filepath) OR is_dir($filepath))

    {

    throw new Exception('The specified file cannot be found!');

    }

    require_once($filepath);

    }

    }

    Indeed, the definition of the above “Loader()” class is so simple to understand that it doesn’t bear any further explanation. Therefore, let me show you another example aimed at illustrating how to use the class, this time for including a pair of PHP files that were created in the preceding article.

    First, here are the aforementioned sample files, called “sample_file1.php” and “sample_file2.php” respectively:

    ('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');

    ?>

    Now that you’ve seen how the two trivial files listed above look, it’s time to code an example that shows how to use the previous “Loader” class to dynamically include these files. Here you have it:

    <?php

     

    try

    {

    // create instance of loader class

    $loader = new Loader();

    // load specified files

    $loader->load('sample_file1.php');

    $loader->load('sample_file2.php');

     

    /* displays the following

    This file has been loaded with the Loader class.

    This file has been loaded at the following time: 2:27:15

    */

    }

    catch (Exception $e)

    {

    echo $e->getMessage();

    exit();

    }

    ?>

    Definitely, from the code sample shown above, it’s extremely easy to grasp the logic that drives the “Loader()” class. Its core “load()” method behaves like an improved proxy for the “require_once()” PHP native function, plus the incorporation of a basic error handling mechanism, in this case implemented via the Exception class bundled with PHP 5.

    So far, nothing spectacular is happening here, right? While the “Loader” class does decent work because it permits us to include several files by dynamically calling its “load()” method, it has a serious drawback. As I mentioned a moment ago, the “load()” method is invoked in the object context, which means that an instance of the “Loader” class has been previously created.

    Is this really necessary? Not at all, believe me! And when I say this, I am only attempting to correctly apply the foundations of object-oriented programming. Since PHP permits us to statically call methods of a class, it’s feasible in this particular case to declare the “load()” method static and include the specified files without having to deal with unnecessary class instances.

    Since it will be a considerable enhancement of the “Loader” class, in the next section I’m going to switch the default declaration of its “load()” to static, to avoid the issue discussed earlier.

    To learn more about how this will be done, click on the link that appears below and keep reading.



     
     
    >>> 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek