How can MVC frameworks in PHP utilize a separate object for request handling via Dependency Injection?

MVC frameworks in PHP can utilize a separate object for request handling by implementing Dependency Injection. This involves injecting the request handling object into the controller or other relevant components, allowing for better separation of concerns and easier testing. By using Dependency Injection, the framework can easily swap out different request handling implementations without changing the core logic of the application.

// Define a RequestHandler interface
interface RequestHandler {
    public function handleRequest();
}

// Create a concrete implementation of the RequestHandler interface
class ConcreteRequestHandler implements RequestHandler {
    public function handleRequest() {
        // Request handling logic here
    }
}

// Inject the RequestHandler object into the controller using Dependency Injection
class Controller {
    private $requestHandler;

    public function __construct(RequestHandler $requestHandler) {
        $this->requestHandler = $requestHandler;
    }

    public function handleRequest() {
        $this->requestHandler->handleRequest();
    }
}

// Create an instance of the ConcreteRequestHandler and inject it into the controller
$requestHandler = new ConcreteRequestHandler();
$controller = new Controller($requestHandler);

// Call the handleRequest method on the controller
$controller->handleRequest();