PHP
  Home arrow PHP arrow Page 2 - Handling File Data with the Facade Pat...
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 
Mobile Linux 
App Generation ROI 
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

Handling File Data with the Facade Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2007-01-23

    Table of Contents:
  • Handling File Data with the Facade Pattern in PHP 5
  • Using the facade pattern to handle file data
  • Applying the facade pattern to a string processor
  • Putting all the classes to work together

  • 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


    Handling File Data with the Facade Pattern in PHP 5 - Using the facade pattern to handle file data


    (Page 2 of 4 )

    As I said in the introduction, before I proceed to create a facade class I’m going to define a simple yet efficient file processing class, which, as you’ll see in a moment, will be capable of reading and writing data to a given text file.

    However, once the file handling class has been created, I’m going to show you how it can be used in tandem with the facade class in question. So, for the moment, I suggest you pay strong attention to the definition for the class, which looks like this: 

    // define 'FileProcessor' class
    class FileProcessor{
        private $dataFile;
        private $fileData;
        public function __construct($dataFile='datafile.txt',
                $data='This string of data will be saved to file!'){
          if(!file_exists($dataFile)){
            throw new Exception('Invalid path for data file!');
          }
          $this->dataFile=$dataFile;
          if(!data){
            throw new Exception('Invalid data to be saved on data file!');
          }
          $this->data=$data;
        }
        // read data from file
        public function readData(){
          if(!$content=file_get_contents($this->dataFile)){
            throw new Exception('Error reading data from data file!');
          }
          return $content;
        }
        // write data to 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!');
          }
          fclose($fp);
        }
    }

    As shown above, the logic implemented by the “FileProcessor” class is indeed easily followed. Basically, all this class does is save an input string to a specified text file, via the respective “writeData()” method, and write the string to the same file, in this particular case by the means of “readData().” So far there's nothing unexpected, right?

    Based upon the functionality offered by this simple file processing class, below I listed a short script, which exemplifies the use of this class. Have a look at the following code sample, please:

    // example using 'FileProcessor' class
    try{
       // instantiate new 'FileProcessor' object
       $fileProc=new FileProcessor();
       // write data to text file
       $fileProc->writeData();
       // read and display data from text file
       echo $fileProc->readData();
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    In this case, the above script shows clearly how the “FileProcessor” class that I created a few lines above can be used in a useful way. As you can see, the previous code snippet first instantiates a new file processor object, then writes a default input string to the specified data file, and finally displays this data on the browser.

    Of course, this example would be rather incomplete if I didn't show you the corresponding output produced by previous script, which is as follows:

    'This string of data will be saved to file!

    All right, at this point I’m quite sure that you understand how the “FileProcessor” class does its thing. I assume you're also intrigued by how this class can be an important piece of the facade pattern. Therefore, in the next section I’m going to create a facade class which will be capable of applying different formats to an inputted string, including all the data read by the file processing class that was covered before.

    To see how this entirely new facade class will be defined, go ahead and read the following section. I’ll be there, waiting for you.

    More PHP Articles
    More By Alejandro Gervasio


       · In this final article of the series, another example on how to implement the facade...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - 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





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