So I was thinking one way that you can bring method chaining into PHP with the built-in global functions would be to "borrow" the |> (pipe) operator from F#. It would pipe the results on the left into the first parameter of the function on the right. Of course PHP would have to revisit the parameter order of some of their functions.
This would allow you to write:
function StrManip($val) {
return str_repeat(strtolower(trim($val)),2);
}
Like this:
function StrManip($val) {
return $val |> trim() |> strtolower() |> str_repeat(2);
}
It looks like what he wants is a String class with methods that mirror the builtin functions. This is a guess, but maybe you could use the __call magic method to do the work like this:
(untested)
class String {
private $_value = '';
public function __construct($value) {
$_value = $value;
}
public function __call ($name, $arguments) {
return new String($name($_value, $arguments));
}
}
$string = new String($string);
echo $string->trim()->strtolower()->str_repeat(2);
You would have to do some parsing to get the $arguments part to work, and it would create a lot of objects when you chain the methods, but in theory this should get you some functionality you are looking for.