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

  • SEARCH
  • Home
  • 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 8123519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:12:28+00:00 2026-06-06T06:12:28+00:00

Why don’t the function handling functions like call_user_func() support passing parameters by reference? The

  • 0

Why don’t the function handling functions like call_user_func() support passing parameters by reference?

The docs say terse things like “Note that the parameters for call_user_func() are not passed by reference.” I assume the PHP devs had some kind of reason for disabling that capability in this case.

Were they facing a technical limitation? Was it a language design choice? How did this come about?

EDIT:

In order to clarify this, here is an example.

<?php

function more(&$var){ $var++; }

$count = 0;
print "The count is $count.\n";

more($count);
print "The count is $count.\n";

call_user_func('more', $count);
print "The count is $count.\n";

// Output:
// The count is 0.
// The count is 1.
// The count is 1.

This is functioning normally; call_user_func does not pass $count by reference, even though more() declared it as a referenced variable. The call_user_func documentation clearly says that this is the way it’s supposed to work.

I am well aware that I can get the effect I need by using call_user_func_array('more', array(&$count)).

The question is: why was call_user_func designed to work this way? The passing by reference documentation says that “Function definitions alone are enough to correctly pass the argument by reference.” The behavior of call_user_func is an exception to that. Why?

  • 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-06-06T06:12:30+00:00Added an answer on June 6, 2026 at 6:12 am

    The answer is embedded deep down in the way references work in PHP’s model – not necessarily the implementation, because that can vary a lot, particularly in the 5.x versions. I’m sure you’ve heard the lines, they’re not like C pointers, or C++ references, etc etc… Basically when a variable is assigned or bound, it can happen in two ways – either by value (in which case the new variable is bound to a new ‘box’ containing a copy of the old value), or by reference (in which case the new variable is bound to the same value box as the old value). This is true whether we’re talking about variables, or function arguments, or cells in arrays.

    Things start to get a bit hairy when you start passing references into functions – obviously the intent is to be able to modify the original variables. Quite some time ago, call-time pass-by-reference (the ability to pass a reference into a function that wasn’t expecting one) got deprecated, because a function that wasn’t aware it was dealing with a reference might ‘accidentally’ modify the input. Taking it to another level, if that function calls a second function, that itself wasn’t expecting a reference… then everything ends up getting disconnected. It might work, but it’s not guaranteed, and may break in some PHP version.

    This is where call_user_func() comes in. Suppose you pass a reference into it (and get the associated the call-time pass-by-reference warning). Then your reference gets bound to a new variable – the parameters of call_user_func() itself. Then when your target function is called, its parameters are not bound where you expect. They’re not bound to the original parameters at all. They’re bound to the local variables that are in the call_user_func() declaration. call_user_func_array() requires caution too. Putting a reference in an array cell could be trouble – since PHP passes that array with “copy-on-write” semantics, you can’t be sure if the array won’t get modified underneath you, and the copy won’t get detached from the original reference.

    The most insightful explanation I’ve seen (which helped me get my head around references) was in a comment on the PHP ‘passing by reference’ manual:

    http://ca.php.net/manual/en/language.references.pass.php#99549

    Basically the logic goes like this. How would you write your own version of call_user_func() ? – and then explain how that breaks with references, and how it fails when you avoid call-time pass-by-reference. In other words, the right way to call functions (specify the value, and let PHP decide from the function declaration whether to pass value or reference) isn’t going to work when you use call_user_func() – you’re calling two functions deep, the first by value, and the second by reference to the values in the first.

    Get your head around this, and you’ll have a much deeper understanding of PHP references (and a much greater motivation to steer clear if you can).

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

Sidebar

Related Questions

Don't be afraid to use any technical jargon or low-level explanations for things, please.
Don't have much to say, just can get into the event handler. XAML: <Grid>
I don't know if this has been asked before, but what i'd like to
I don't like Jackson. I want to use ajax but with Google Gson. So
Don't know much about encryption... Say I'm preparing a SAML request to submit to
don't know if this is possible.. I'm using sqlite3 schema: CREATE TABLE docs (id
Don't think there's much to say, here's my code for (int i = 0;
don't know if the title describes anything about what I'm trying to say but
Don't know much about running a function on every item in an array, still
Don't be scared of the extensive code. The problem is general. I just provided

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.