__callStatic(), call_user_func_array(), references, and PHP 5.3.1

后端 未结 4 1359
温柔的废话
温柔的废话 2021-01-05 01:35

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

4条回答
  •  难免孤独
    2021-01-05 02:06

    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.

提交回复
热议问题