What is dependency injection in PHP?

Dependency injection is a design pattern where objects receive their dependencies from external sources rather than creating them internally. This approach promotes loose coupling, improves testability, and makes code more maintainable by avoiding hard-coded dependencies.

There are several approaches to inject dependencies into objects. Here are the most commonly used methods −

Constructor Injection

In this approach, dependencies are injected through the class constructor when the object is instantiated −

<?php
class Programmer {
    private $skills;
    
    public function __construct($skills){
        $this->skills = $skills;
    }
    
    public function totalSkills(){
        return count($this->skills);
    }
    
    public function getSkills(){
        return implode(', ', $this->skills);
    }
}

$createskills = array("PHP", "JQUERY", "AJAX");
$p = new Programmer($createskills);
echo "Total skills: " . $p->totalSkills() . "<br>";
echo "Skills: " . $p->getSkills();
?>
Total skills: 3
Skills: PHP, JQUERY, AJAX

Setter Injection

In setter injection, dependencies are provided through setter methods after object creation −

<?php
class Profile {
    private $languages = array();
    
    public function setLanguages($languages){
        $this->languages = $languages;
    }
    
    public function getLanguages(){
        return implode(', ', $this->languages);
    }
    
    public function getLanguageCount(){
        return count($this->languages);
    }
}

$profile = new Profile();
$languages = array("Hindi", "English", "French");
$profile->setLanguages($languages);

echo "Languages: " . $profile->getLanguages() . "<br>";
echo "Total languages: " . $profile->getLanguageCount();
?>
Languages: Hindi, English, French
Total languages: 3

Interface Injection

This method uses interfaces to define how dependencies should be injected −

<?php
interface DatabaseInterface {
    public function connect();
}

class MySQLDatabase implements DatabaseInterface {
    public function connect(){
        return "Connected to MySQL database";
    }
}

class UserService {
    private $database;
    
    public function setDatabase(DatabaseInterface $database){
        $this->database = $database;
    }
    
    public function getConnection(){
        return $this->database->connect();
    }
}

$database = new MySQLDatabase();
$userService = new UserService();
$userService->setDatabase($database);

echo $userService->getConnection();
?>
Connected to MySQL database

Benefits of Dependency Injection

  • Loose Coupling: Classes don't create their own dependencies, making them more flexible
  • Testability: Easy to inject mock objects during unit testing
  • Maintainability: Changes to dependencies don't affect the dependent classes
  • Reusability: Components can be easily reused with different dependencies

Conclusion

Dependency injection is essential for writing maintainable PHP applications. Constructor injection is preferred for required dependencies, while setter injection works well for optional dependencies. This pattern significantly improves code testability and flexibility.

Updated on: 2026-03-15T08:14:56+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements