__callStatic(), call_user_func_array(), references, and PHP 5.3.1
Asked Answered
O

4

9

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.

Order answered 9/4, 2011 at 2:59 Comment(1)
It appears that the issue is more to do with the __call() magic than call_user_func_array()... Furthermore, it doesn't seem there's a particularly clean workaround. Still, any thoughts are welcome.Order
B
3

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.

Barbarbarbara answered 9/4, 2011 at 3:53 Comment(5)
Thanks Charles; Yea, I figured that the call magic would be the problem. I've noticed it's popped up in the PHP bugfix requests, yet has not been patched (from what I've found at least) Explicitly passing the arguments by reference of course throws a warning as you've noted, and unfortunately that wouldn't be satisfactory solution anyways. What sort of hoop jumping did you have in mind, no matter how insane?Order
Unfortunately the only thing I can come up with would be not using __callStatic at all, which probably isn't going to help you much here. Can you tell us more about what you're trying to accomplish?Barbarbarbara
Well, it's become an academic trial now, but the purpose initially comes from this question I posted earlier; #5601383 The first proposed option to handle autoloading unclassed function libraries takes advantage of __callStatic. Though it may be beneficial for optimization purposes to go a different route, I'd still like to solve this, as it (to me at least) seems like a very clean solution.Order
Hm, interesting idea, though I'm not sure if PHP is the right language to pull off that kind of magic. If this was Perl or Ruby, I'm sure it could be done...Barbarbarbara
Yea, I was also hoping I could take advantage of this functionality for other purposes. The autoloading classless function libraries was a start, but I was rolling around some other ideas for a lightweight educational MVC framework I'm developing, and this would have been an asset. I'll keep hunting. Thanks again Charles, if any other ideas come to mind, I'd be happy to hear them.Order
O
2

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.

Order answered 10/5, 2014 at 12:19 Comment(0)
S
0

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.

Stefansson answered 9/4, 2011 at 4:32 Comment(1)
Thanks Wh1T3h4Ck5; We've sifted it down to being an issue of calling magic. __call()/__callStatic() don't carry a reference into their defined bodies via the argument array, at least not by any conventional means. The function definition for the call magic (or any magic function as per docs) furthermore does not permit passing the arguments by reference.Order
L
0

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

Laplante answered 5/6, 2018 at 12:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.