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

后端 未结 4 1344
温柔的废话
温柔的废话 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 01:47

    I'm answering this myself with a step-by-step reproduction of the issue, which demonstrates that it is likely not possible to achieve the desired functionality.

    Our test function is fairly straightforward, and is as follows:

    function testFunction(&$argument)
    {
        $argument++;
    }
    

    If we call the function directly, it, of course, behaves as expected:

    $value = 1;
    testFunction($value);
    var_dump($value); // int(2)
    

    If we call the function using call_user_func_array:

    call_user_func_array('testFunction', [$value]);
    var_dump($value); // int(1)
    

    The value remains at 1, because $value was not passed by reference. We can force this:

    call_user_func_array('testFunction', [&$value]);
    var_dump($value); // int(2)
    

    Which again works as calling the function directly.

    Now, we introduce the class, using __callStatic:

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

    If we call the function by forwarding it through __callStatic:

    TestClass::testFunction($value);
    var_dump($value); // int(1)
    

    The value remains at 1, and we also get a warning:

    Warning: Parameter 1 to testFunction() expected to be a reference, value given

    Which verifies that $argumentArray passed into __callStatic does not contain references. That makes perfect sense, as PHP doesn't know the arguments are intended to be accepted by reference in __callStatic, nor that we are forwarding the arguments on.

    If we change __callStatic to accept the array by reference, in an attempt to force the desired behaviour:

    public static function __callStatic($name, &$argumentArray)
    {
        return call_user_func_array($name, $argumentArray);
    }
    

    Uh oh! Spaghetti-O's!

    Fatal error: Method TestClass::__callstatic() cannot take arguments by reference

    Can't do that; besides, that's supported by the documentation anyway:

    Note:
    None of the arguments of these magic methods can be passed by reference.

    Despite not being able to declare __callStatic to accept by reference, if we use call_user_func_array (which would be rather awkward and self-defeating in practice) to forward references through __callStatic:

    call_user_func_array(['TestClass', 'testFunction'], [&$value]);
    var_dump($value); // int(2)
    

    It once again works.

    0 讨论(0)
  • 2021-01-05 01:47

    After years I had a moment of serendipity:

    class TestClass{
        public static function __callStatic($functionName, $arguments) {
            $arguments[0]->{'ioParameter'} = 'two';
        }
    }
    $objectWrapper = new StdClass();
    $objectWrapper->{'ioParameter'} = 'one';
    TestClass::testFunction($objectWrapper);
    var_dump($objectWrapper); 
    // outputs: object(stdClass)#3 (1) { ["ioParameter"]=> string(3) "two" }
    

    (tested on php 5.5)

    This workaround is clean and viable if you control the interfaces.

    It works by wrapping arguments in an object, which by language definition, is always passed around "by reference".

    It allows for passing writable (reference) parameters even using __callStatic (and possibly __call).

    0 讨论(0)
  • 2021-01-05 02:03

    Wow, after more than hour of blowing my mind, I'm still not sure but...

    mixed call_user_func_array ( callback $function , array $param_arr )
    

    If we use simple logic (in this situation) when class calls function call_user_func_array() it uses static strings as parameters (passed by magic method __callStatic()), so parameter 2 is a string in eyes of call_user_func_array() function and that parameter is also parameter 1 for function testFunction(). It's simply string not a pointer to variable, and that probably is a reason for message like:

    Warning: Parameter 1 to testFunction() expected to be a reference, value given in ... on line ...
    

    In other words, it just can't assign value back to variable that doesn't exist but still, testFunction() expects parameter to be a reference and throws warning because it's not.

    You can test it out of class like:

    function testFunction(&$arg){
        $arg .= 'world';
      }
    
    $string = 'hello';
    call_user_func_array('testFunction', array($string));
    echo $string;
    

    It throws same Warning! So problem is not with class, problem is this function.

    This function obviously passes function parameters as static data without pointing to variables and functions it calls just can not handle referenced arguments.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题