4 ms·
You know that you have to supply the function by _name_ as a string?
by eru 19y ago
You 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.