Content · Web Development · PHP

Refactoring Legacy PHP Globals into Injectable Services

Globals hide dependencies and make isolated tests harder. Constructor injection moves required collaborators into explicit, replaceable interfaces.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Globals hide dependencies and make isolated tests harder. Constructor injection moves required collaborators into explicit, replaceable interfaces.

What It Does


The legacy implementation depends on hidden mutable state. Constructor injection exposes the dependency and enables substitution.

Requirements and Compatibility


Use a supported PHP runtime and install any third-party tool or extension explicitly shown in the examples. Commands should be run from the project root unless stated otherwise.

Syntax, Inputs and Results


The examples separate configuration, the smallest working operation and a realistic follow-up task. Adapt file paths and dependency versions to your own project.

Example 1: Basic Use


The legacy implementation depends on hidden mutable state.

<?php
$GLOBALS['taxRate'] = 0.2;
function legacyTotal(int $base): float {
    return $base * (1 + $GLOBALS['taxRate']);
}
echo legacyTotal(100);

Example 2: Another Case


Constructor injection exposes the dependency and enables substitution.

<?php
interface TaxPolicy { public function total(int $base): int; }
final class FixedTax implements TaxPolicy {
    public function total(int $base): int { return $base + intdiv($base * 20, 100); }
}
final class InvoiceService {
    public function __construct(private TaxPolicy $tax) {}
    public function calculate(int $base): int { return $this->tax->total($base); }
}
echo (new InvoiceService(new FixedTax()))->calculate(100);

Practical Example


Tests can replace the collaborator without modifying global state.

<?php
interface TaxPolicy { public function total(int $base): int; }
final class NoTax implements TaxPolicy {
    public function total(int $base): int { return $base; }
}
final class InvoiceService {
    public function __construct(private TaxPolicy $tax) {}
    public function calculate(int $base): int { return $this->tax->total($base); }
}
$service = new InvoiceService(new NoTax());
echo $service->calculate(100);

For production, apply the compatibility and edge-case limits described below.

Important Behavior and Edge Cases


Do not simply wrap global access in another global helper; separate responsibilities and pass dependencies at object creation.

Comparison with Related Functions and Tools


SubjectKey difference
Direct function calls vs injectable servicesConstructor injection exposes collaborators so they can be replaced during testing.

Common Beginner Mistakes


MistakeCorrect approach
Relying on a demonstration as production validationAdd explicit error handling and test all inputs that can change behavior.
Skipping the prerequisite of this specific workflowInstall or configure the named tool and confirm the example can actually run.
Ignoring edge conditionsDo not simply wrap global access in another global helper; separate responsibilities and pass dependencies at object creation.

Key Takeaways


  • Globals hide dependencies and make isolated tests harder. Constructor injection moves required collaborators into explicit, replaceable interfaces.
  • Do not simply wrap global access in another global helper; separate responsibilities and pass dependencies at object creation.
  • Keep application configuration, source code and validation tests under version control.

Use these examples as a starting point, then validate the result with the actual PHP build, dependencies and inputs used by your application.

Share this resourceSend this page to someone who may find it useful.