PHP
  Home arrow PHP arrow Page 2 - Processing File Data with Template Cla...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
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: 3 stars3 stars3 stars3 stars3 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · Over the course of this final installment of the series, what you’re going to learn...
     

       

    PHP ARTICLES

    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP





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