3 ms·
> I'm not sure why they're adding the array_column function Isn't the function call much more readable than the comprehension? At least as a beginner, I found
by geon 14y ago
> I'm not sure why they're adding the array_column function
Isn't the function call much more readable than the comprehension?
At least as a beginner, I found the large set of built in functions to be one of the strongest points of PHP.
- Smerity 14y agoOne major disadvantage with so many functions is the cluttered global namespace[1]. PHP only added namespaces in 5.3.0 and can't modularize their globals into namespaces without causing massive backwards compatibility problems. PHP is stuck with a polluted global namespace, I'd be wary of adding more. Mind you, I'm well aware that this feeling could just be as I'm primarily a Python coder -- in PHP, you perform array_keys($xyz) whilst in Python it's xyz.keys(). Whilst you might consider Python as cheating by pushing all the functions onto the objects, it results in a far happier global namespace[2]. As far as list comprehensions, I tend to find list comprehensions the readable and self documenting, at least in Python where I most commonly see them. If nothing else, what is the ordering of the arguments for the equivalent non list comprehension? With list comprehensions you don't have that impedance. // Python example with filter vs list comprehension // Filter requires you to remember the ordering -- // and does it filter on True or False? filter(lambda x: x % 2 == 0, range(10)) --> 0 2 4 6 8 // The list comprehension is self documenting [x for x in range(10) if x % 2 == 0] --> 0 2 4 6 8 I can also convert the majority of list comprehensions into English quite easily -- "output x for each x in range(10) if x is divisible by 2" (though it may be cleaner if it was "for each x in range(10) [output x] if x is divisible by 2"). [1]: http://php.net/quickref.php http://php.net/quickref.php [2]: http://docs.python.org/library/functions.html http://docs.python.org/library/functions.html + http://docs.python.org/library/exceptions.html http://docs.python.org/library/exceptions.html
- MarceloRamos 14y agoYes what the code does is very clear, but the function call is even a level up clearer, maybe you don't want to know how it does it's job, you just want an array of values in a column (array_column(array, column)). Personally I like comprehensions, but I tend to disguise them behind a function, it's much more clear to me. (but if I need the code to be super fast, I avoid using an extra function.)
- TazeTSchnitzel 14y ago>PHP only added namespaces in 5.3.0 and can't modularize their globals into namespaces without causing massive backwards compatibility problems. PHP is stuck with a polluted global namespace They could do what Python did for 3.0, and make a tool similar to Python's 2to3 that replaces global namespace references.
- sirclueless 14y agoThat sounds like a disaster. I don't think anyone wants to follow Python's lead on breaking backwards compatibility without a very good reason. A much better reason than a cluttered namespace.
- Tloewald 14y agoIf they're going to allow queries on arrays of lists -- not a bad idea -- why not simply implement a SQL subset? I can see a huge collection of new array functions...
- geon 14y agoSomething like PHPlinq? http://phplinq.codeplex.com/wikipage?title=Examples&referringTitle=Home http://phplinq.codeplex.com/wikipage?title=Examples&refe...