site-icon

Arjit Sharma


Dependency Injection in PHP

When our class need object of other class.


Suppose we have classes : logger(displays a message)   UserProfile(Needs to use logger's log function)

3 steps : Initializing Dependency(Line 23),Injecting Dependency(Line 24) and Using Dependency(Line 27).

class logger{
public function log($msg){
    echo "Log : $msg";
  }
}

class UserProfile{
  public $logger;//This variable will store object of logger
  
  public function __construct($logger){
    $this->logger=$logger;//logger variable points to object of logger 
  }  

  public function createUser(){
    $this->logger->log("User Created");//using variable to acces method of logger
  }

  public function deleteUser(){
    $this->logger->log("User Deleted");
  }
}

$logger=new logger();//Initialize dependecy
$profile=new UserProfile($logger);//Inject Dependecy via constructor
$profile->createUser();
$profile->deleteUser();
$profile->logger->log("Hello");//using dependecy