class MyController { private $database; public function __construct() { $this->database = new Database(); } public function index() { // Use $this->database to fetch data from the database } }
class MyController { private $logger; public function __construct(Logger $logger) { $this->logger = $logger; } public function index() { // Use $this->logger to log messages } }In this example, the __construct method is used to initialize a private $logger property with an instance of the Logger class. The Logger class is defined as a dependency in the constructor, which means that it must be passed in as a parameter when creating a new instance of the controller. Package library: This example uses a custom Logger class, which could be part of a larger package library or framework.