Home arrow PHP arrow Page 4 - Working with CSS Styles and the Stage Pattern in PHP 5

Completing the implementation of the stage pattern - PHP

The stage pattern lets you build classes that can modify their behaviors according to the variations of a given programming context. If you want to learn more about it, then you should start reading this article now! Welcome to the final installment of the series that began with "Implementing the stage pattern in PHP 5." Made up of two articles, this series walks you through the application of this useful pattern, and complements the corresponding theory with illustrative code samples.

TABLE OF CONTENTS:
  1. Working with CSS Styles and the Stage Pattern in PHP 5
  2. Building a target class
  3. Defining a basic contextual class
  4. Completing the implementation of the stage pattern
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
April 25, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Indeed, the best way to understand how the stage pattern works with this specific implementation is by creating a functional example that demonstrates how a given web document can use different CSS styles according to specific format requirements.

In this case, the following sample pair of scripts show how an instance of the contextual "CSScontext" class is capable of modifying the behavior of a specific "WebPage" object to generate two different versions of the same web document.

Now that I have explained how the pertinent example is going to work, please examine the corresponding source code, which looks like this: 

// example displaying a normal web page
try{
   // create new 'CSSContext' object 
   $cssContext=new CSSContext();
   // set CSS styles to work with a normal web page format
   $cssContext->setPageFormat('normal');
   // create new 'WebPage' object
   $webPage=new WebPage($cssContext->getCSS());
   $webPage->makeHeader();
   $webPage->makeBody();
   $webPage->makeFooter();
   // display web page using normal format
   echo $webPage->getHTML();
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

// example displaying a printer-friendly web page
try{
   // create new 'CSSContext' object 
   $cssContext=new CSSContext();
   // set CSS styles to work with a printer-friendly web page
format
   $cssContext->setPageFormat('print');
   $webPage=new WebPage($cssContext->getCSS());
   $webPage->makeHeader();
   $webPage->makeBody();
   $webPage->makeFooter();
   // display web page using printer-friendly format
   echo $webPage->getHTML();
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

As shown above, the stage pattern is implemented here to change the behavior of a "WebPage" object so it can display distinct versions of the same web document. As you can see in the two previous scripts, the first time the web page is rendered, a "normal" CSS style is used, while in the second case, the page in question is displayed with a "printer-friendly" format.

At this point, I believe that the previous code samples should give you an accurate idea of how to implement the stage pattern in a concrete situation. However, as with many other design patterns, I suggest you create your own testing examples to acquire a better grounding in how it this pattern functions.

Final thoughts

Unfortunately, we've come to the end of this journey. In these two tutorials, I introduced the basic concepts of the stage pattern, and also showed you a couple of functional examples where it can be applied with minor hassles.

As you hopefully learned, if you ever need to build a class that changes the way it works according to the modifications introduced into its context, the stage pattern can provide a good method for solving this issue.

See you in the next PHP tutorial!



 
 
>>> 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: