In PHP, how can I convert a string into a sequence of function calls?

Solution :

You can split the string by . and then call each function in sequence, passing the previous result as the argument:


$string = '12345.find_user.find_last_name';

// Break the string into parts
$functions = explode('.', $string);

// First element is the initial argument
$arg = array_shift($functions);

// Call each function in order, passing the result forward
foreach ($functions as $function) {
    $arg = $function($arg);
}

echo $arg;

🔹 How it works:

explode(‘.’, $string) splits the string into an array:

[“12345”, “find_user”, “find_last_name”]

array_shift($functions) takes the first element (12345) as the starting argument.

The foreach loop iteratively calls each function (find_user, then find_last_name) with the updated $arg.