PHP
  Home arrow PHP arrow Page 3 - Building 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

Building Loader Apps in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2009-05-27


    Table of Contents:
  • Building Loader Apps in PHP
  • Start building loading programs with a require_once() function
  • Building a basic file 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


    Building Loader Apps in PHP - Building a basic file loader class
    ( Page 3 of 4 )

    As I stated previously, developing a file loading system that abstracts the load process a bit further is a straightforward procedure that can be tackled with minimal efforts. Basically, this system will base its functionality on a simple loader class that will behave as a proxy for a “require_once()” function. 

    Imagine now that there are a couple of files that need to be included by a certain PHP application, whose definitions are as follows:

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

    ?>

    Again, it’s fair to say that I’m not going to waste your time (and mine) discussing how the above files do their things. Instead, pay close attention to the signature of the file loading class below, which not surprisingly has been called “Loader().” It looks like this:

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

    }

    }

    Despite its rather basic definition, the previous “Loader()” class is a considerable breakthrough when it comes to building a file loading mechanism. It not only acts like a wrapper for the “require_once()” function, but it handles the eventual errors that might occur during the load process in a more elegant way, through native PHP exceptions.

    At this point, everything is starting to look a bit better. I created a basic loader class, and there are a couple of sample files that need to be included within a fictional application to display some trivial messages on screen.

    The question that comes up now is: how can these two files be loaded by the previous “Loader” class? Well, it’s quite possible that you’ve already found the answer, but in the last section of this tutorial I’m going to create a hands-on example that will demonstrate how to use this class in a few simple steps.

    To learn more about how this example will be developed, go ahead and read the next segment.



     
     
    >>> 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