3 ms·
I 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
by sapphirecat 19y ago
I 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?