Home arrow PHP arrow Page 2 - Introducing Bridge Classes with PHP 5

Introducing the basics of the bridge pattern - PHP

The bridge class, or what's commonly known as the bridge pattern, lets you create a class with its abstract functionality and implementation residing on different class hierarchies. This lets you decouple the class from its concrete application. This article, the first of three parts, introduces you to the bridge pattern and its uses.

  1. Introducing Bridge Classes with PHP 5
  2. Introducing the basics of the bridge pattern
  3. Implementing the bridge pattern’s core logic
  4. Implementing the logic of a bridge class in a different hierarchy level
  5. Seeing the bridge pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
January 03, 2007

print this article



Naturally, a good place to start demonstrating the creation and usage of bridge objects with PHP 5 is with developing a practical example where these types of objects are utilized to perform a given task.

In this case, the example that I’m going to set up will be based upon creating first a target object, called “Message,” which will be tasked with displaying some trivial strings to the browser. Next, I’m going to define a bridge class that will be capable of saving the mentioned target object to different storage locations, like a plain text file, an array structure and a simple cookie.

Hopefully, after you see how this bridge class looks, you’ll realize how its instantiation can be separated from its corresponding implementation, since both things will reside on different class hierarchies.

So far, so good. The process for building a bridge class seems really straightforward, therefore let me show you the first target class involved in this creation process. As I said before , it is named “Message,” and its simple signature is shown below:

// define 'Message' class (target object for 'BridgeObjectSaver' class) class Message{ private $message; public function __construct($message='Default Message'){ if(!$message||!is_string($message)||strlen
($message)>255){ throw new Exception('Invalid input message'); } $this->message=$message; } public function getMessage(){ return $this->message; } }

As illustrated above, the “Message” sample class is really simple to understand. All this class does is accept an input string as the unique parameter, which will be returned to calling code with the trivial purpose of being displayed on the browser, or receiving similar processing. Extremely easy, right?

Well, I have to admit that the previous target class is rather primitive, but for my purpose of showing you how a bridge class can be created with PHP 5, the definition for this class is more than enough, at least for now.

Therefore, having shown the signature of the respective “Message” class, it’s a good time to leap forward and learn how to build a bridge class. This class will use “Message” objects and save them to different storage locations, including text files, arrays and cookies respectively.

Do you want to see how this brand new bridge class will be created? All right, jump into the following section and keep reading.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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