PHP
  Home arrow PHP arrow Page 3 - 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 - A static file loading method
    ( Page 3 of 4 )

    As I anticipated in the segment that you just read, one of the simplest ways to use the “Loader” class without having to create an instance of it consists of calling its “load()” method out of the object context, or in other words, statically.

    PHP does support calling a non-static method statically (which is not supported when using static class properties), but to assure that the “load()” method will always be invoked statically, I’m going to refactor the class, this time declaring the method obviously static.

    Does all of this jargon sound a bit confusing to you? Fear not. Only pay attention to the enhanced version of the “Loader” class, after implementing this subtle change. Here it is:

    // define the loader class (the 'load()' method is declared static)

    class Loader

    {

    // constructor (not implemented)

     public function __construct(){}

     

    // load specified file

    public static function load($filepath)

    {

    if (!file_exists($filepath))

    {

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

    }

    require_once($filepath);

    }

    }

    That was easy to code and read, wasn’t it? As you can see, now the “Loader” class looks nearly identical to its previous version, except for one small but crucial modification: its “load()” method has been declared static explicitly via the “static” keyword. This means that if the method is eventually called in the object scope (read dynamically), the PHP engine will complain loudly about this and throw an error.

    Naturally, the most evident benefit in declaring this method static is that there’s no need to spawn an instance of the “Loader” class to call it. In this case, I’m getting the best of both worlds: I’m still able to include files using the “load()” method, but no object needs to be created.

    However, the best way to understand the nitty-gritty of this process is by coding a concrete example. So, the last section of this tutorial will be dedicated to develop that example, so you can see how the improved version of the “Loader” class is really much more flexible and efficient.

    Thus, don’t waste more time; read the following segment. It’s only one click away.



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