PHP
  Home arrow PHP arrow Page 2 - Processing File Data with Template Classes in PHP 5
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

Processing File Data with Template Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2007-03-26


    Table of Contents:
  • Processing File Data with Template Classes in PHP 5
  • Building a basic file processing class
  • Creating a simple template class
  • Building a few simple subclasses
  • Seeing the template pattern 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


    Processing File Data with Template Classes in PHP 5 - Building a basic file processing class
    ( Page 2 of 5 )

    A good point to start demonstrating an approachable implementation of the template pattern consists of defining a basic file processing class, which will be used later on by a template object to display contents fetched from a specific target file.

    As you'll see, the file contents will be displayed either in (X)HTML and XML format or as an uppercased string, depending on which template class will be used for this purpose. So far there's nothing unexpected, right?

    Now that you know the reasons for using a template object, please pay attention to the signature of the file processing class. It looks like this:

    // define 'FileProcessor' class
    class FileProcessor{
       private $dataFile;
      
    private $data;
      
    public function __construct($dataFile='default_file.txt',
                   $data='This is the default data.'){
        
    if(!file_exists($dataFile)){
          
    throw new Exception('A valid data file must be
    provided!');
         
    }
        
    if(!is_string($data)){
          
    throw new Exception('File data must be a string!');
        
    }
        
    $this->dataFile=$dataFile;
        
    $this->data=$data;
      
    }
      
    // write data to target file
      
    public function writeData(){
        
    if(!$fp=fopen($this->dataFile,'w')){
          
    throw new Exception('Error opening data file!');
        
    }
        
    if(!fwrite($fp,$this->data)){
          
    throw new Exception('Error writing data to file!');
        
    }
        
    flose($fp);
      
    }
      
    // read data from target file
      
    public function readData(){
        
    if(!$contents=file_get_contents($this->dataFile)){
          
    throw new Exception('Error reading from data file!');
        
    }
        
    return $contents;
      
    }
      
    // get size of data file
      
    public function getFileSize(){
        
    if(!$size=filesize($this->dataFile)){
          
    throw new Exception('Error retrieving size of data
    file!'); 
        
    }
        
    return $size;
      
    }
    }

    As illustrated above, the logic followed by the previous "FileProcessor" class is indeed very easy to understand. In crude terms, all that this class does is read and write data to a specified target file, in addition to determining the size of this file by using its simple "getFileSize()" method.

    However, as you might have guessed, the previous class isn't the main objective of this tutorial at all. As I said before, I'm going to use an instance of it, so it can be adequately processed by a template object. Sounds pretty simple, doesn't it?

    Of course, as you know, the signature of this template class remains undefined, so if you want to see how the template in question will look, please click on the link below and keep reading.



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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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