4 ms·
Fun is that PHP still lets you pretend it works functionally. http://php.net/array_map http://php.net/array_map http://php.net/array_reduce http://php.net/arr
by aston 19y ago
Fun is that PHP still lets you pretend it works functionally.
http://php.net/array_map http://php.net/array_map
http://php.net/array_reduce http://php.net/array_reduce
http://php.net/array_filter http://php.net/array_filter
http://php.net/array_walk http://php.net/array_walk
http://php.net/call_user_func_array http://php.net/call_user_func_array (apply)
http://php.net/create_function http://php.net/create_function (lambda)
PHP is a language that encompasses all useful things in all other languages. Which, I guess, makes it inferior.
- eru 19y agoYou know that you have to supply the function by _name_ as a string?
- sapphirecat 19y agoI don't get what you're saying. aston did say pretend. Once you have the function name (or an object method array) it can be stored in a variable with any name you want, passed around, etc. You can pretend it's a function all you want; you can check whether it seems like a real function with is_callable; and you can call it via call_user_func or call_user_func_array (this distinction made necessary because PHP lacks Python/Ruby *array unpacking syntax). Somewhere I have a PHP class that lets you fake closure. You call Closure::Create($real_cb, $state_data) and it gives you a callback that, when called, calls $real_cb with $state_data plus all arguments that it was called with. And if $state_data is an array whose elements were assigned by reference, the 'closed-over' state is mutable. So overall, it's a good pretense, even if foreach will always be faster than functional style.
- jamongkad 19y agoSimulating closures? that seems pretty clever I've just recently integrated a functional extension into our private web application framework a few months ago. Perhaps we can share notes?
- aston 19y agoWell, since PHP by default reinterprets bare-strings as actual strings, you can just type the name without quotes, and things should still work. Until you define() a constant with the same name.
- eru 19y agoI do not mind the inconvenience of typing in quotes. I just do not like that function are not treated as first class citizens in this respect. There may be ways around this in newer versions of PHP, though. By the way - can someone please try something like the following: $width = getimagesize($filename) [0]; The last time I checked, PHP threw a _Parser_Error_ for indexing an array that you just got from a function. Not even arrays feel like first class citizens. (Pardon me, if this ward has been eliminated in the mean time. I just vividly remember losing some time hunting for a bug because of this 'feature'.)
- aston 19y agoYeah, the [] operator works on a variable name, not on the variable itself. Kind of silly.
- pjf 19y agoPlease don't call "create_function" a "lambda". That's a parody of anonymous function. You have to escape (\") your source code, function is registered in the global scope, and the only reference you get on it is... a string with generated, unique function name.
- jamongkad 19y agoYeah create_function is a half ass attempt for the use of lambda functions. I would not recommend using it.