--- url: "https://refactoring.guru/design-patterns/bridge/php/example" titulek: "Bridge in PHP / Design Patterns" h1: "Bridge in PHP" popis: "Bridge pattern in PHP. Full code example in PHP with detailed comments and explanation. Bridge is a structural design pattern that divides business logic or huge class into separate class hierarchies that can be developed independently." jazyk: "en" kanonicka: "https://refactoring.guru/design-patterns/bridge/php/example" drobecky: ["Home", "Design Patterns", "Bridge", "PHP"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:27:29" znaku: 11324 slov: 1516 kodovani: "utf-8" sha256: "52b8fa4f30822d42ab8096c1429cfd4341d3fac50bb6eb980ed9c42bffe9160b" --- # **Bridge** in PHP **Bridge** is a structural design pattern that divides business logic or huge class into separate class hierarchies that can be developed independently. One of these hierarchies (often called the Abstraction) will get a reference to an object of the second hierarchy (Implementation). The abstraction will be able to delegate some (sometimes, most) of its calls to the implementations object. Since all implementations will have a common interface, they’d be interchangeable inside the abstraction. [Learn more about Bridge](/design-patterns/bridge) **Complexity:** **Popularity:** **Usage examples:** The Bridge pattern is especially useful when supporting multiple types of database servers or working with several API providers of a certain kind (for example, cloud platforms, social networks, etc.) **Identification:** Bridge can be recognized by a clear distinction between some controlling entity and several different platforms that it relies on. ## Conceptual Example This example illustrates the structure of the **Bridge** design pattern and focuses on the following questions: - What classes does it consist of? - What roles do these classes play? - In what way the elements of the pattern are related? After learning about the pattern’s structure it’ll be easier for you to grasp the following example, based on a real-world PHP use case. #### **index.php:** Conceptual example ``` implementation = $implementation; } public function operation(): string { return "Abstraction: Base operation with:\n" . $this->implementation->operationImplementation(); } } /** * You can extend the Abstraction without changing the Implementation classes. */ class ExtendedAbstraction extends Abstraction { public function operation(): string { return "ExtendedAbstraction: Extended operation with:\n" . $this->implementation->operationImplementation(); } } /** * The Implementation defines the interface for all implementation classes. It * doesn't have to match the Abstraction's interface. In fact, the two * interfaces can be entirely different. Typically the Implementation interface * provides only primitive operations, while the Abstraction defines higher- * level operations based on those primitives. */ interface Implementation { public function operationImplementation(): string; } /** * Each Concrete Implementation corresponds to a specific platform and * implements the Implementation interface using that platform's API. */ class ConcreteImplementationA implements Implementation { public function operationImplementation(): string { return "ConcreteImplementationA: Here's the result on the platform A.\n"; } } class ConcreteImplementationB implements Implementation { public function operationImplementation(): string { return "ConcreteImplementationB: Here's the result on the platform B.\n"; } } /** * Except for the initialization phase, where an Abstraction object gets linked * with a specific Implementation object, the client code should only depend on * the Abstraction class. This way the client code can support any abstraction- * implementation combination. */ function clientCode(Abstraction $abstraction) { // ... echo $abstraction->operation(); // ... } /** * The client code should be able to work with any pre-configured abstraction- * implementation combination. */ $implementation = new ConcreteImplementationA(); $abstraction = new Abstraction($implementation); clientCode($abstraction); echo "\n"; $implementation = new ConcreteImplementationB(); $abstraction = new ExtendedAbstraction($implementation); clientCode($abstraction); ``` #### **Output.txt:** Execution result ``` Abstraction: Base operation with: ConcreteImplementationA: Here's the result on the platform A. ExtendedAbstraction: Extended operation with: ConcreteImplementationB: Here's the result on the platform B. ``` ## Real World Example In this example, the `Page` hierarchy acts as the **Abstraction**, and the `Renderer` hierarchy acts as the **Implementation**. Objects of the `Page` class can assemble web pages of a particular kind using basic elements provided by a `Renderer` object attached to that page. Since both of the class hierarchies are separate, you can add a new `Renderer` class without changing any of the `Page` classes and vice versa. #### **index.php:** Real world example ``` renderer = $renderer; } /** * The Bridge pattern allows replacing the attached Implementation object * dynamically. */ public function changeRenderer(Renderer $renderer): void { $this->renderer = $renderer; } /** * The "view" behavior stays abstract since it can only be provided by * Concrete Abstraction classes. */ abstract public function view(): string; } /** * This Concrete Abstraction represents a simple page. */ class SimplePage extends Page { protected $title; protected $content; public function __construct(Renderer $renderer, string $title, string $content) { parent::__construct($renderer); $this->title = $title; $this->content = $content; } public function view(): string { return $this->renderer->renderParts([ $this->renderer->renderHeader(), $this->renderer->renderTitle($this->title), $this->renderer->renderTextBlock($this->content), $this->renderer->renderFooter() ]); } } /** * This Concrete Abstraction represents a more complex page. */ class ProductPage extends Page { protected $product; public function __construct(Renderer $renderer, Product $product) { parent::__construct($renderer); $this->product = $product; } public function view(): string { return $this->renderer->renderParts([ $this->renderer->renderHeader(), $this->renderer->renderTitle($this->product->getTitle()), $this->renderer->renderTextBlock($this->product->getDescription()), $this->renderer->renderImage($this->product->getImage()), $this->renderer->renderTextBlock('$' . number_format($this->product->getPrice(), 2)), $this->renderer->renderLink("/cart/add/" . $this->product->getId(), "Add to cart"), $this->renderer->renderFooter() ]); } } /** * A helper class for the ProductPage class. */ class Product { private $id, $title, $description, $image, $price; public function __construct( string $id, string $title, string $description, string $image, float $price ) { $this->id = $id; $this->title = $title; $this->description = $description; $this->image = $image; $this->price = $price; } public function getId(): string { return $this->id; } public function getTitle(): string { return $this->title; } public function getDescription(): string { return $this->description; } public function getImage(): string { return $this->image; } public function getPrice(): float { return $this->price; } } /** * The Implementation declares a set of "real", "under-the-hood", "platform" * methods. * * In this case, the Implementation lists rendering methods that can be used to * compose any web page. Different Abstractions may use different methods of the * Implementation. */ interface Renderer { public function renderTitle(string $title): string; public function renderTextBlock(string $text): string; public function renderImage(string $url): string; public function renderLink(string $url, string $title): string; public function renderHeader(): string; public function renderFooter(): string; public function renderParts(array $parts): string; } /** * This Concrete Implementation renders a web page as HTML. */ class HTMLRenderer implements Renderer { public function renderTitle(string $title): string { return "
Add to cart
JSON view of a simple content page, with the same client code:
{
"title": "Star Wars, episode1",
"text": "A long time ago in a galaxy far, far away...",
"img": "/images/star-wars.jpeg",
"link": {"href": "/cart/add/123", "title": "Add to cart"}
}
```
## **Bridge** in Other Languages