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

Using Self-Saving Objects with Command Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 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:
      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


    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


       · Over the course of this article of the series, some command classes are utilized for...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - 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
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




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