Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6059281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:41:35+00:00 2026-05-23T08:41:35+00:00

Is it possible to pass functions by reference? Something like this: function call($func){ $func();

  • 0

Is it possible to pass functions by reference?

Something like this:

function call($func){
    $func();
}

function test(){
    echo "hello world!";
}

call(test);

I know that you could do 'test', but I don’t really want that, as I need to pass the function by reference.

Is the only way to do so via anonymous functions?

Clarification: If you recall from C++, you could pass a function via pointers:

void call(void (*func)(void)){
    func();
}

Or in Python:

def call(func):
    func()

That’s what i’m trying to accomplish.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:41:36+00:00Added an answer on May 23, 2026 at 8:41 am

    For what it’s worth, how about giving something like this a shot? (Yes, I know it’s an anonymous function which was mentioned in the post, but I was disgruntled at the abundance of replies that did not mention closures/function-objects at all so this is mostly a note for people running across this post.)

    I don’t use PHP, but using a closure appears to work in PHP 5.3 (but not PHP 5.2) as demonstrated here. I am not sure what the limitations, if any, there are. (For all I know the closure will eat your children. You have been warned.)

    function doIt ($fn) {
      echo "doIt\n";
      return $fn();
    }
    
    function doMe () {
      echo "doMe\n";
    }
    
    // I am using a closure here.
    // There may be a more clever way to "get the function-object" representing a given
    // named function, but I do not know what it is. Again, I *don't use PHP* :-)
    echo doIt(function () { doMe(); });
    

    Happy coding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to pass a reference to a function to another function in
Is it possible to pass a function/callback from javascript to a java applet? For
Is it possible to pass command line arguments into a function from within a
Is it possible to pass parts of a linq Query into a function? I
Possible Duplicates: When pass-by-pointer is preferred to pass-by-reference in C++? Are there benefits of
Possible Duplicates: “const T &arg” vs. “T arg” How to pass objects to functions
With PHP is it even Possible to Pass arrays by Reference ? or its
Possible Duplicates: How to pass objects to functions in C++? is there any specific
Possible Duplicate: FAQ : How to pass objects to functions in C++? Pointer vs.
Possible Duplicate: How to pass objects to functions in C++? My main program gives

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.