What are some best practices for capturing object method calls with parameters from a template using regular expressions in PHP?

When capturing object method calls with parameters from a template using regular expressions in PHP, it is important to have a robust regex pattern that can handle various scenarios such as different parameter types, multiple parameters, and nested method calls. One approach is to use named capturing groups in the regex pattern to easily extract the method name and parameters. Additionally, it is recommended to use the preg_match_all function to capture all occurrences of method calls in the template.

$template = "Hello {User->getName()}! Your age is {User->getAge(25)}.";

$regex = '/\{(?P<method>[a-zA-Z]+->[a-zA-Z]+)\((?P<params>.*?)\)\}/';

preg_match_all($regex, $template, $matches, PREG_SET_ORDER);

foreach ($matches as $match) {
    $method = $match['method'];
    $params = explode(',', $match['params']);
    
    // Process the method call with parameters
    // Example: $result = $User->$method($params[0], $params[1], ...);
}