PHP
  Home arrow PHP arrow Page 2 - Handling File Data with the Facade Pattern 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? 
Google.com  
PHP

Handling File Data with the Facade Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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
     

       

    PHP ARTICLES

    - 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...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek