How can Dependency Injection be utilized to avoid the need for including helper classes in every PHP file?

When using Dependency Injection, we can create a single instance of the helper class and inject it into the classes that need it, rather than including the helper class in every PHP file. This helps to avoid cluttering the codebase with unnecessary includes and promotes better code organization and reusability.

// Helper class
class Helper {
    public function doSomething() {
        return "Doing something useful";
    }
}

// Class using Dependency Injection
class MyClass {
    private $helper;

    public function __construct(Helper $helper) {
        $this->helper = $helper;
    }

    public function useHelper() {
        echo $this->helper->doSomething();
    }
}

// Create an instance of the helper class
$helper = new Helper();

// Instantiate the class with the injected helper
$myClass = new MyClass($helper);

// Use the helper method
$myClass->useHelper();