-
July 19th, 2005, 03:53 PM
#1
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
#2
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.
-
July 19th, 2005, 04:10 PM
#3
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
#4
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|