What are common methods for dynamically populating a switch case function in PHP?

When dynamically populating a switch case function in PHP, one common method is to use an associative array where the keys represent the cases and the values represent the corresponding actions. This allows for a more flexible and scalable way to handle multiple cases without having to hardcode each one individually.

// Define an associative array with cases and their corresponding actions
$cases = [
    'case1' => function() {
        echo 'Action for case1';
    },
    'case2' => function() {
        echo 'Action for case2';
    },
    // Add more cases and actions as needed
];

// Get the case value dynamically
$case = 'case1';

// Check if the case exists in the array and execute the corresponding action
if (array_key_exists($case, $cases)) {
    $cases[$case]();
} else {
    echo 'Case not found';
}