When would you use a Factory pattern versus a Singleton pattern in a PHP backend?
Last updated
Factory: centralizes creation and picks which concrete class to build at runtime, returning a new object each call (e.g. a notification channel chosen by config).
Singleton: guarantees exactly one shared instance for the app lifecycle (e.g. config registry, logger, connection pool).
Singletons act as global state and hurt testability. In Laravel prefer container binding via
$app->singleton()over a static implementation.