Results 1 to 4 of 4

Thread: PHP - Function References

  1. #1
    Senior Member
    Join Date
    Jun 2002
    Posts
    174

    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.
    I\'m back.

  2. #2
    AO Antique pwaring's Avatar
    Join Date
    Aug 2001
    Posts
    1,409
    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.
    Paul Waring - Web site design and development.

  3. #3
    Senior Member
    Join Date
    Jun 2002
    Posts
    174
    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".
    I\'m back.

  4. #4
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    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
  •