Early previewThis guide is in development. Items tagged verify are still being checked against official docs.
nebx Learn
GitHub
Menu
Home/PHP/Anti-patterns
PHP

PHP

Pragmatic server-side language that powers a large share of the web.

Paradigm
OOP, procedural, functional features
Typing
Dynamic with gradual typing (strict_types)
Memory
Reference counting + cycle collector
Runs
Zend Engine, bytecode + OPcache, optional JIT
Concurrency
Shared-nothing per request; Fibers (8.1)

Anti-patterns

Solutions that look reasonable but cause problems. First the PHP-specific traps, then the classics that apply everywhere.

PHP-specific

Anti-pattern
Do this instead
✕ Concatenating user input into SQL
✓ Bind parameters with PDO prepared statements; SQL injection remains a top web vulnerability.
✕ Mixing HTML, SQL and logic in one file
✓ Separate layers: controller, service, template engine (Twig, Blade).
✕ Silencing errors with @
✓ Handle the failure explicitly and log it.
✕ global variables
✓ Inject dependencies through constructors or the framework container.
✕ Fat controllers
✓ Move business rules into services or domain objects; controllers translate HTTP only.

Universal anti-patterns

God object

One class knows and does too much. Split it by responsibility (SRP).

Spaghetti code

Tangled control flow with no structure. Refactor into small functions with clear inputs and outputs.

Golden hammer

Using one familiar tool or pattern for every problem. Pick the simplest fit.

Premature optimisation

Optimising before measuring. Profile first, then fix the real bottleneck.

Copy-paste programming

Duplicated logic drifts apart. Extract shared code once the duplication is real (rule of three).

Magic numbers and strings

Unexplained literals. Use named constants or enums.

Singleton abuse

Global state disguised as a pattern. Prefer dependency injection with a single-instance lifetime.

Lava flow

Dead or experimental code nobody dares to remove. Delete it; version control remembers.

Saved in this browser only. Part of From code to production (0/5).