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 4252000
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:44:12+00:00 2026-05-21T04:44:12+00:00

I’ve been reading around on SO, and elsewhere, however I can’t seem to find

  • 0

I’ve been reading around on SO, and elsewhere, however I can’t seem to find anything conclusive.

Is there any way to effectively carry references through this call stack, resulting in the desired functionality as described in the example below? While the example doesn’t try to solve it, it certainly illustrates the problem:

class TestClass{
    // surely __call would result similarly
    public static function __callStatic($function, $arguments){
        return call_user_func_array($function, $arguments);
    }
}

// note argument by reference
function testFunction(&$arg){
    $arg .= 'bar';
}

$test = 'foo';
TestClass::testFunction($test);

// expecting: 'foobar'
// getting:   'foo' and a warning about the reference
echo $test;

For the sake of inspiring a potential resolution, I’m going to add summary details here:

Focusing on only call_user_func_array(), we can determine that (at least with PHP 5.3.1) you cannot implicitly pass arguments by reference:

function testFunction(&$arg){
    $arg .= 'bar';
}

$test = 'foo';
call_user_func_array('testFunction', array($test));
var_dump($test);
// string(3) "foo" and a warning about the non-reference parameter

By explicitly passing the array element $test as a reference, we can alleviate this:

call_user_func_array('testFunction', array(&$test));
var_dump($test);
// string(6) "foobar"

When we introduce the class with __callStatic(), an explicit call-time parameter by reference seems to carry through as I expected, but deprecation warnings are issued (in my IDE):

class TestClass{
    public static function __callStatic($function, $arguments){
        return call_user_func_array($function, $arguments);
    }
}

function testFunction(&$arg){
    $arg .= 'bar';
}

$test = 'foo';
TestClass::testFunction(&$test);
var_dump($test);
// string(6) "foobar"

Omission of the reference operator in TestClass::testFunction() results in $test being passed by value to __callStatic(), and of course is passed by value as an array element to testFunction() via call_user_func_array(). This results in a warning, since testFunction() expects a reference.

Hacking around, some additional details have surfaced. The __callStatic() definition, if written to return by reference (public static function &__callStatic()) has no visible effect. Furthermore, recasting the elements of the $arguments array in __callStatic() as references we can see that call_user_func_array() works somewhat as expected:

class TestClass{
    public static function __callStatic($function, $arguments){
        foreach($arguments as &$arg){}
        call_user_func_array($function, $arguments);
        var_dump($arguments);
        // array(1) {
        //   [0]=>
        //   &string(6) "foobar"
        // }
    }
}

function testFunction(&$arg){
    $arg .= 'bar';
}

$test = 'foo';
TestClass::testFunction($test);
var_dump($test);
// string(3) "foo"

These results are expected, as $test is no longer passed by reference, the change is not passed back into it’s scope. However, this confirms that call_user_func_array() is in fact working as expected, and that the issue is certainly confined to the calling magic.

Upon further reading, it appears it may be a “bug” in PHP’s handling of user functions, and the __call()/__callStatic() magic. I’ve perused the bug database for existing or related issues, and had found one, but haven’t been able to locate it again. I’m considering issuing another report, or a request for the existing report to be reopened.

  • 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-21T04:44:13+00:00Added an answer on May 21, 2026 at 4:44 am

    This is a fun one.

    TestClass::testFunction(&$string);
    

    This works, but it also throws a call-time-pass-by-reference warning.

    The main issue is that __callStatic‘s second argument comes in by value. It’s creating a copy of the data, unless that data is already a reference.

    We can duplicate the calling error thusly:

    call_user_func_array('testFunction', array( $string ));
    // PHP Warning:  Parameter 1 to testFunction() expected to be a reference, value given
    
    call_user_func_array('testFunction', array( &$string ));
    echo $string;
    // 'helloworld'
    

    I tried to modify the __callStatic method to copy the array deeply by reference, but that didn’t work either.

    I’m pretty sure that the immediate copy caused by __callStatic is going to be a killer here, and that you won’t be able to do this without enough hoop jumping to make it a bit sticker, syntax-wise.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.