Search code examples
phpphp-7comma-operator

Does PHP 7 make easier to substitute comma operator?


I have learned so far that PHP lacks comma operator (I am not here for opinions if it is good or bad). Since I I fall into pattern of such expressions:

($tmp = bar(), foo($tmp), $tmp)
  1. perform computation and cache the output
  2. use the cached value (call a function with given value)
  3. return the cached value

I can substitute the comma operator with calling a custom function which takes value and lambda, and returns the value.

But I wonder -- maybe PHP 7 brought some new feature that makes substitution easier or allows to substitute all forms of comma operator expressions?


Solution

  • Not quite, but since PHP 7 you can easily create and call a lambda function at the same time, thanks to the new AST:

    (function() { $tmp = bar(); foo($tmp); return $tmp; })();