Null Object Pattern
Also Known As
null object pattern
NullObject
do-nothing object
TL;DR
Replace null with an object that implements the expected interface but performs no operation, eliminating null checks throughout the codebase.
Explanation
The Null Object pattern (Bobby Woolf) provides a do-nothing implementation of an interface that can be used wherever a null check would otherwise be required. For example, a NullLogger that implements LoggerInterface but discards all messages replaces if ($logger !== null) $logger->log(...) checks everywhere. The Null Object makes the absence of something explicit and type-safe, simplifies calling code, and follows Tell Don't Ask. PHP 8+ Null Object are often combined with union types and nullsafe operators as complementary approaches.
Common Misconception
✗ Null object pattern is just a fancy way to avoid null checks. It eliminates entire branches of conditional logic by providing a safe default behaviour — code that calls methods on a NullObject works without branching, making it structurally simpler.
Why It Matters
A null object implements the same interface as a real object but does nothing — it eliminates null checks at call sites and makes the absence of a thing an explicit, safe concept.
Common Mistakes
- Not implementing the full interface — callers that use methods not on the null object still get null errors.
- Null objects that return null from methods — they should return safe defaults (empty string, 0, empty array).
- Using null objects where an Optional/Maybe type better communicates the possible absence.
- Applying null objects everywhere — sometimes null carries meaning that should be handled, not silently swallowed.
Code Examples
✗ Vulnerable
// Null checks spread throughout call sites
$discount = $user->getDiscount();
$price = $discount !== null ? $price * (1 - $discount->rate()) : $price;
✓ Fixed
interface Discount {
public function apply(float $price): float;
}
class PercentDiscount implements Discount {
public function __construct(private float $rate) {}
public function apply(float $price): float { return $price * (1 - $this->rate); }
}
class NullDiscount implements Discount {
public function apply(float $price): float { return $price; } // no-op
}
// Now: no null check needed at call sites
$price = $user->getDiscount()->apply($price);
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
8
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Google 4
Perplexity 2
Ahrefs 1
Also referenced
How they use it
crawler 4
crawler_json 3
Related categories
⚡
DEV INTEL
Tools & Severity
🟢 Low
⚙ Fix effort: Low
⚡ Quick Fix
Create a NullLogger or GuestUser that implements the same interface as the real object but does nothing — callers never need to check for null
📦 Applies To
PHP 5.0+
web
cli
queue-worker
🔗 Prerequisites
🔍 Detection Hints
Repeated null checks: if ($logger !== null) $logger->log() — candidate for Null Object pattern
Auto-detectable:
✓ Yes
phpstan
psalm
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: Medium
Context: Class
Tests: Update