Early previewThis guide is in development. Items tagged verify are still being checked against official docs.
nebx Learn
GitHub
Menu
PHP

PHP

Pragmatic server-side language that powers a large share of the web.

Paradigm
OOP, procedural, functional features
Typing
Dynamic with gradual typing (strict_types)
Memory
Reference counting + cycle collector
Runs
Zend Engine, bytecode + OPcache, optional JIT
Concurrency
Shared-nothing per request; Fibers (8.1)
Structural pattern

Facade

Intent

Provides a simplified interface to a library, framework or other complex set of classes.

Problem

Converting a video needs a dozen classes from a codec library in the right order.

Solution

A facade class exposes the few operations clients need and handles the complexity inside.

Real-world analogy

Think of it like this

Ordering by phone: one operator talks to the warehouse, payment and delivery for you.

Structure

association / extends implements / depends on
Client 4 Facade - linksToSubsystemObjects+ subsystemOperation() 1 AdditionalFacade + anotherOperation() 2 SubsystemA SubsystemB 3 SubsystemC

Hover or tap a numbered role to highlight it in the diagram.

  1. 1Facade — Facade gives convenient access to a particular part of the subsystem.
  2. 2AdditionalFacade — Additional Facades keep one facade from becoming a god object.
  3. 3SubsystemB — The Complex Subsystem is dozens of objects that don't know the facade exists.
  4. 4Client — Client calls the facade instead of the subsystem objects directly.

Code example

A PHP code sample for Facade is coming soon. Factory Method, Singleton, Decorator, Observer and Strategy already have code in all six languages.

Applicability

Use Facade when you need a limited, straightforward interface to a complex subsystem.

Use Facade when structuring a subsystem into layers.

Pros and cons

Pros

  • Isolates code from subsystem complexity

Cons

  • Can become a god object coupled to everything

Relations with other patterns

Check yourself

Check yourself

Expose one simple convert(video, format) call over a complex codec library.

Saved in this browser only. Part of Structural patterns (0/7).