PHP
  Home arrow PHP arrow Page 4 - Using Self-Saving Objects with Command Objects 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

Using Self-Saving Objects with Command Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2006-12-26


    Table of Contents:
  • Using Self-Saving Objects with Command Objects in PHP 5
  • Defining a core module of the command pattern
  • Storing and loading self-saving objects
  • Completing the command pattern
  • Seeing the command 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


    Using Self-Saving Objects with Command Objects in PHP 5 - Completing the command pattern
    ( Page 4 of 5 )

    In consonance with the concepts that I deployed a few lines above, the only piece that remains still undefined is the class that creates commanded objects. In this particular case, a commanded object must expose two concrete methods, called “save()” and load()” respectively, which will perform the saving and loading operations on the object itself.

    Perhaps the previous definition sounds a little bit confusing, but in fact any possible doubts that you may have will be clarified if you take a look at the signature of the mentioned commanded class. Following only my common sense, I called this class “ObjectCommanded,” and its source code is as follows:

    // define 'ObjectCommanded'class
    
    class ObjectCommanded{
    
    	private $objectPath;
    
    	public function __construct($objectPath){
    
    		$this->setObjectPath($objectPath);
    	}
    
    	public function setObjectPath($objectPath){
    
    		if(!file_exists($objectPath)){
    
    			throw new Exception('Invalid target file to 
    save object!'); } $this->objectPath=$objectPath; } // auto save commanded object public function save(){ if(!$fp=fopen($this->objectPath,'w')){ throw new Exception('Error opening target
    file!');                 }                 if(!fwrite($fp,serialize($this))){ throw new Exception('Error writing target
    file!'); } fclose($fp); } // auto load commanded object public function load(){ if(!$obj=unserialize(file_get_contents
    ($this->objectPath))){ throw new Exception('Error loading object
    from target file!'); } return $obj; } }

    If you examine the above class, you’ll grasp quickly what it does. As you can see, this “ObjectCommanded” class is capable of saving an instance of itself to a given text file, aside from loading this instance from the mentioned file. All these processes are performed via a serialization-unserialization sequence, something that is clearly demonstrated by the definition of the corresponding “load()” and “save()” methods.

    Nevertheless, the most interesting aspect concerning the implementation of these methods has to do with the intrinsic definition for the command pattern, since they encapsulate all the logic necessary for execution in the respective commanders. Now, do you see how a self-saving object can be programmatically instructed by the two previous command classes? I guess you do!

    All right, at this point you hopefully learned not only how the object command classes were properly defined, but how the respective commanded class was created too. Thus, with all these elements at our disposal, certainly it’d be very instructive if we can develop a practical example that integrates all the pieces together, implementing the already familiar command pattern.

    That’s exactly what I’m going to do in the final section of this article, therefore I suggest you click on the below link and keep reading.



     
     
    >>> 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek