How can PHP classes be utilized to efficiently handle dynamic content display based on URL parameters?

To efficiently handle dynamic content display based on URL parameters using PHP classes, we can create a class that parses the URL parameters and determines which content to display based on those parameters. By using a class, we can encapsulate the logic for handling different URL parameters in a structured and reusable way.

<?php

class ContentHandler {
    public function displayContent() {
        $urlParams = $_GET;
        
        if(isset($urlParams['page'])) {
            $page = $urlParams['page'];
            
            switch($page) {
                case 'home':
                    // Display home page content
                    echo "Welcome to the home page!";
                    break;
                case 'about':
                    // Display about page content
                    echo "Learn more about us!";
                    break;
                default:
                    // Handle unknown page
                    echo "Page not found";
            }
        }
    }
}

// Instantiate the class and call the displayContent method
$contentHandler = new ContentHandler();
$contentHandler->displayContent();

?>