0
0
PHPprogramming~20 mins

Dependency injection concept in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dependency Injection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the main benefit of dependency injection?

Consider a PHP class that needs to use a database connection. What is the main benefit of using dependency injection to provide the database connection to the class?

AIt makes the class tightly coupled to a specific database implementation.
BIt allows the class to receive the database connection from outside, making it easier to test and change dependencies.
CIt allows the class to create the database connection internally whenever needed.
DIt forces the class to manage the lifecycle of the database connection.
Attempts:
2 left
💡 Hint

Think about how receiving dependencies from outside helps with testing and flexibility.

component_behavior
intermediate
2:00remaining
What will this PHP code output when using dependency injection?

Given the following PHP code, what will be the output?

PHP
<?php
class Logger {
    public function log($msg) {
        echo "Log: $msg";
    }
}

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();
?>
AFatal error: Uncaught Error: Class 'Logger' not found
BUser created
CLog:
DLog: User created
Attempts:
2 left
💡 Hint

Look at how the logger is passed to the User class and used in the create method.

📝 Syntax
advanced
2:00remaining
Which option correctly injects a dependency in PHP constructor?

Which of the following PHP code snippets correctly injects a dependency into a class constructor?

Aclass Service { public function __construct(Database $db) { $this->db = $db; } }
Bclass Service { public function __construct($db) { $db = $this->db; } }
Cclass Service { function __construct(Database $db) { $this->db == $db; } }
Dclass Service { public function __construct(Database $db) { $db->this = $this; } }
Attempts:
2 left
💡 Hint

Check the assignment direction and parameter typing.

🔧 Debug
advanced
2:00remaining
Why does this dependency injection code cause an error?

Given this PHP code, why does it cause a fatal error?

PHP
<?php
class Mailer {}
class Notification {
    private $mailer;
    public function __construct() {
        $this->mailer = new Mailer();
    }
}

$notification = new Notification(new Mailer());
?>
ANotification constructor does not accept any parameters but is called with one.
BMailer class is not defined before use.
CNotification class tries to inject Mailer but does not assign it.
DMailer class constructor requires parameters but none are given.
Attempts:
2 left
💡 Hint

Look at the constructor signature and how the object is instantiated.

lifecycle
expert
2:00remaining
How does dependency injection affect object lifecycle in PHP?

In PHP, when using dependency injection, which statement best describes how the lifecycle of injected objects is managed?

AInjected objects are always created inside the dependent class and destroyed with it.
BInjected objects must be static to survive beyond the dependent class lifecycle.
CInjected objects are created outside and can be shared or reused independently of the dependent class lifecycle.
DInjected objects are cloned automatically when injected to avoid shared state.
Attempts:
2 left
💡 Hint

Think about who creates the injected object and how that affects its lifetime.