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

后端 未结 4 1375
温柔的废话
温柔的废话 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

    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).

提交回复
热议问题