Global State: Variables accessible from any procedure. Procedural code often relies on shared global state, which is its biggest weakness — unexpected mutations are hard to trace. Global state is data that any part of a program can read and change, such as global variables or static properties that act like them. It is convenient, because nothing needs to be passed around.
It is also a common source of bugs. When a value changes unexpectedly, any line in the codebase could be responsible. Tests become fragile too, because one test can leave global state behind that affects the next.
What should you keep in mind with Global State?
Modern frameworks reduce the need for it with dependency injection and request-scoped services. Long-running PHP setups like Laravel Octane or queue workers make it more dangerous, because static data survives from one request or job to the next. Pass data explicitly where you can, and treat any shared mutable state as something to justify and document.
Implement proven backend architecture patterns including Clean Architecture, Hexagonal Architecture, and Domain-Driven Design. Use this skill when designing clean architecture for a new microservice, when refactoring a monolith to use bounded contexts, when implementing hexagonal or onion architecture patterns, or when debugging dependency cycles between application layers.