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.
Printable View
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.
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:
http://www.phpbuilder.com/lists/php3...99903/0527.php
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.
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".
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.
Slarty