4 ms·
Anonymous first-class functions in PHP? I can't say that was high on my list of things I would classify as necessary to make it usable.
by gdp 17y ago
Anonymous first-class functions in PHP? I can't say that was high on my list of things I would classify as necessary to make it usable.
- jcapote 17y agoBefore you had to write: function is_not_numeric($num){ !is_numeric($num); } array_map('is_not_numeric', array(1,3,5,'a','c',3)); The fact that php used to take a string as the callback and not an expression is seriously broken. Now (presumably), one can write: array_map(function($x){ !is_numeric($x) }, array(1,3,5,'a','c',3)); Although, ideally it would be: array_map(!is_numeric, array(1,3,5,'a','c',3));
- gdp 17y agoRight, I'm not arguing that anonymous first-class functions are useful, I'm just saying that I think there are so many other things that are seriously broken with PHP as to make this akin to worrying about having enough clean forks for a dinner party while your house is on fire. I'm not just PHP-trolling here, either. There is a history in the PHP project of adding features to the language without any reference to how they will be made "safe". The PHP interpreter is notoriously unhelpful at catching even the simplest of bugs. This just seems like another ad-hoc feature added to the list of things that are likely to introduce subtle bugs that are undetectable until they are running on a production system somewhere.
- mgreenbe 17y agoSeriously. Anonymous functions without a sane lexical system is asking for trouble, though marking closure variables will mitigate the problems a little. I agree that PHP's house is on fire, but PHP has such a big house that I'm not sure it's noticed yet.
- alexfarran 17y agoIt's on my list! If we're doing a feature comparison of web scripting languages it was obviously lacking from PHP and present in one form or another in everything else. Not sure about the use syntax but at least it makes it clear what variables are closed over.
- KrisJordan 17y agoThe use syntax is a little weird at first but it turns out to be straightforward to understand. In fact, being explicit about what is being closed over makes the function itself easier to understand. PHP, being the quirky language that it is, needs the use syntax to enable closures for cases where there are dynamic variable references. This is a bad example but illustrates the problem: $foo = 'bar'; $fun = function() use ($foo) { $var = 'foo'; echo ${$var}; }; $fun(); // output 'bar' $fun = function() { $var = 'foo'; echo ${$var}; }; $fun(); // fails Sure, you could close over the entire local namespace but that's an efficiency nightmare and leads to unpredictable code.