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

Seeing the command pattern in action - 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

As I expressed earlier, to see how the previously defined command classes along with the respective commanded object can be put to work together, it’s necessary to prepare a practical example where all the pieces fit together.

Given that, below I coded a simple script. It shows clearly how the two object commanders are capable of instructing the commanded class on when to save an instance of itself to a specified text file, as well as when to perform an auto-loading process. Have a look at the following code sample, please:

try{
// instantiate 'ObjectCommanded' object
$objCom=new ObjectCommanded('default_path/object.txt');
// instantiate 'SaveObjectCommand' object
$saveObjCom=new SaveObjectCommand($objCom);
// execute 'save()' method in commanded object (commanded 
object is saved to target file) $saveObjCom->executeCommand(); // instantiate 'LoadObjectCommand' object $loadObjCom=new LoadObjectCommand($objCom); // execute 'load()' method in commanded object (commanded
object is loaded from target file) $loadedObj=$loadObjCom->executeCommand(); } catch(Exception $e){ echo $e->getMessage(); exit(); }

As shown above, after instantiating the corresponding commanded object, the two “SaveObjectCommand” and “LoadObjectCommand” commanders simply invoke their “executeCommand()” methods, which trigger the respective auto-saving and auto-loading operations in the object in question.

In addition, I strongly recommend that you experiment with all the classes that were developed in this article so that you can acquire a more solid background in how to apply the command pattern within your own PHP applications.

Final thoughts

Sadly, we’ve come to the end of this series. However, I hope this journey has been instructive, particularly if you’re interested in expanding your skills in the terrain of pattern-based PHP programming. More specifically, if the command pattern that you learned here has caught your attention, don’t hesitate to use it in your future projects. See you in the next PHP tutorial!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap

Dev Shed Tutorial Topics: