Skip to Content
PHPCODE
Dependency Injection
oops code / August 12, 2018

<?php

class Logger
{
public function log($message)
{
echo “Logging message: $message”;
}
}
class UserProfile {

private $logger;
public function createUser()
{
//create user.
$this->logger->log(“User Created.”);
}
public function updateUser()
{
//update User
$this->logger->log(“user Update.”);
}
public function deleteUser()
{
//delete user
$this->logger->log(“user delete.”);
}
public function __construct(Logger $logger)
{
$this->logger =$logger;
}
}
$logger=new Logger();
$profile=new UserProfile($logger);
$profile->createUser();
$profile->updateUser();
?>

PHPCODE © 2024