Home arrow PHP arrow Page 4 - Using Self-Saving Objects with Command Objects in PHP 5

Completing the command pattern - PHP

If you were looking for an approachable guide on how to create and use command objects with PHP 5, then look no further, because your search is over. Welcome to the final part of the series “Creating command objects with PHP 5.” Comprised of three comprehensive tutorials, this series walks you through the basics of how to apply the command pattern in PHP, and it accompanies its corresponding theory with numerous code samples.

TABLE OF CONTENTS:
  1. Using Self-Saving Objects with Command Objects in PHP 5
  2. Defining a core module of the command pattern
  3. Storing and loading self-saving objects
  4. Completing the command pattern
  5. Seeing the command pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
December 26, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: