StructuralPHPverifiedVerified

Facade Pattern in PHP

Provides a simplified, unified interface to a complex subsystem, hiding its internal complexity from clients.

How to Implement the Facade Pattern in PHP

1Step 1: Define complex subsystem classes

class VideoDecoder
{
    public function decode(string $filename): string { return "decoded:{$filename}"; }
}

class AudioMixer
{
    public function mix(string $audio): string { return "mixed:{$audio}"; }
}

class SubtitleRenderer
{
    public function render(string $subs): string { return "rendered:{$subs}"; }
}

class VideoEncoder
{
    public function encode(string $video, string $audio, string $subs): string
    {
        return "output.mp4";
    }
}

2Step 2: Create a simple facade that hides subsystem complexity

class VideoConverterFacade
{
    private VideoDecoder $decoder;
    private AudioMixer $mixer;
    private SubtitleRenderer $subtitles;
    private VideoEncoder $encoder;

    public function __construct()
    {
        $this->decoder = new VideoDecoder();
        $this->mixer = new AudioMixer();
        $this->subtitles = new SubtitleRenderer();
        $this->encoder = new VideoEncoder();
    }

    public function convert(string $filename): string
    {
        $video = $this->decoder->decode($filename);
        $audio = $this->mixer->mix($filename);
        $subs = $this->subtitles->render($filename);
        return $this->encoder->encode($video, $audio, $subs);
    }
}

// Usage — one simple call instead of managing four subsystems
$converter = new VideoConverterFacade();
echo $converter->convert('movie.avi'); // "output.mp4"

Facade Pattern Architecture

hourglass_empty

Rendering diagram...

lightbulb

Facade Pattern in the Real World

A hotel concierge is a facade for the city’s complex infrastructure. Instead of you directly calling a taxi company, booking a restaurant, and arranging a museum ticket separately, the concierge handles all of it through a single conversation. The underlying services still exist in their full complexity—you just don’t deal with them directly.