How can a method be dynamically called by name in PHP?

To dynamically call a method by name in PHP, you can use the call_user_func() function. This function takes the method name as a string and calls it with the specified parameters. This allows you to call a method based on a variable or dynamically generated method name.

class MyClass {
    public function myMethod() {
        echo "Hello World!";
    }
}

$methodName = "myMethod";
$obj = new MyClass();
call_user_func([$obj, $methodName]);