© Kelt Dockins 2017

Kelt Dockins, Design Patterns in PHP and Laravel, 10.1007/978-1-4842-2451-9_12

12. Decorator

Kelt Dockins

(1)Dolph, Arkansas, USA

$> git checkout decorator

Intent

Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.1

Applications

Decorators extend functionality. How are decorators different than mere inheritance? Classical inheritance extends functionality of classes before runtime. Decorators extend functionality of real-time objects during runtime. The decorator wraps an real object, giving us the ability to change object behavior at runtime. It may seem weird to talk about runtime and PHP, since PHP is an interrupted language and not ...

Get Design Patterns in PHP and Laravel now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.