© Kelt Dockins 2017

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

23. State

Kelt Dockins

(1)Dolph, Arkansas, USA

$> git checkout state$> composer update

Intent

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.1

Applications

The state pattern is a glove pattern. When it fits right, it fits just right. In other words, you’ll know when you need it. You can use state pattern for modeling things that change behavior during the course of its lifetime.

Ever find yourself using some string variable named $type and a giant cluster of switch/case/if/else statements to determine which methods to call? The state pattern will clean up all those nasty conditions. A ...

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.