0
0
PHPprogramming~10 mins

Dependency injection concept in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Dependency injection concept
Create Dependency Object
Inject Dependency into Class
Class Uses Dependency
Perform Action with Dependency
Output Result
This flow shows how a dependency object is created, injected into a class, and then used by that class to perform an action.
Execution Sample
PHP
<?php
class Logger {
  public function log($msg) { echo "Log: $msg\n"; }
}

class User {
  private $logger;
  public function __construct(Logger $logger) { $this->logger = $logger; }
  public function create() { $this->logger->log('User created'); }
}

$logger = new Logger();
$user = new User($logger);
$user->create();
?>
This PHP code creates a Logger object, injects it into a User class, and calls a method that uses the Logger to output a message.
Execution Table
StepActionEvaluationResult
1Create Logger object$logger = new Logger()Logger instance created
2Create User object with Logger$user = new User($logger)User instance created with Logger injected
3Call create() on User$user->create()Calls Logger->log('User created')
4Logger logs messageLogger->log('User created')Outputs: Log: User created
5End of scriptNo more codeScript ends
💡 Script ends after User->create() outputs the log message
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$loggernullLogger instanceLogger instanceLogger instanceLogger instance
$usernullnullUser instance with LoggerUser instance with LoggerUser instance with Logger
Key Moments - 2 Insights
Why do we pass the Logger object into the User constructor instead of creating it inside User?
Passing Logger into User allows us to control which Logger User uses and makes User easier to test and reuse. See execution_table step 2 where User is created with Logger injected.
What happens if we don't inject Logger and User tries to create it internally?
User would be tightly linked to Logger, making it hard to change Logger behavior or test User independently. Dependency injection avoids this by injecting Logger from outside.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of $user after step 2?
ANull
BUser instance with Logger injected
CLogger instance
DUser instance without Logger
💡 Hint
Check the 'Result' column in step 2 of the execution_table
At which step does the Logger output the message 'Log: User created'?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look at the 'Action' and 'Result' columns in step 4 of the execution_table
If we did not inject Logger into User, which problem would most likely occur?
AUser would be tightly coupled to Logger, reducing flexibility
BLogger would not be created
CUser cannot log messages
DLogger would log twice
💡 Hint
Refer to key_moments about why injection is important
Concept Snapshot
Dependency Injection in PHP:
- Create dependency object (e.g., Logger)
- Pass it into class constructor (e.g., User)
- Class uses injected dependency
- Enables loose coupling and easier testing
- Avoids creating dependencies inside classes
Full Transcript
Dependency injection means giving a class the objects it needs from outside instead of making them inside. In PHP, we create a Logger object first, then pass it to the User class constructor. User stores this Logger and uses it to log messages. This way, User does not create Logger itself, making the code more flexible and easier to test. The execution steps show creating Logger, injecting it into User, calling User's create method, which uses Logger to output a message, and then ending the script.