July 19th, 2005, 03:53 PM
PHP - Function References
In C, you can pass around references to functions, store them in arrays, etc. Does anyone know if this is possible in PHP? I've heard of passing variables by reference, but not functions.
July 19th, 2005, 04:00 PM
I don't think you can pass around references to functions as such, but you can use variable interpolation to an extent to achieve that effect:
That's from PHP 3 though, no idea if that behaviour still works.
Out of interest, why would you want to do this anyway? I've been using PHP for several years and have never come across a situation where I'd need to pass in a reference to a function.
July 19th, 2005, 04:10 PM
I'm trying to achieve some semblance of polymorphism with PHP4 (which I don't believe supports it natively). I was able to achieve this in C using a homemade object-C implementation, some tricky memory alignment, and the passing of function references to the "parent".
July 19th, 2005, 11:24 PM
Functions are not first-class objects in PHP. There is no function type, and you can't create truly anonymous functions.
The only way to create higher-order functions, is to pass around the function name and use call_user_func (or whatever it's called).
The PHP standard library contains some higher order functions itself, and it does exactly that.