php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #40694 __call() does not allow passing args by reference
Submitted: 2007-03-02 17:27 UTC Modified: 2007-09-15 01:00 UTC
Votes:36
Avg. Score:4.8 ± 0.6
Reproduced:33 of 33 (100.0%)
Same Version:17 (51.5%)
Same OS:17 (51.5%)
From: andrei@php.net Assigned:
Status: No Feedback Package: Feature/Change Request
PHP Version: 5CVS-2007-03-02 (CVS) OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: andrei@php.net
New email:
PHP Version: OS:

 

 [2007-03-02 17:27 UTC] andrei@php.net
Description:
------------
__call() method does not allow specifying the arguments array by reference. Essentially this means that there is no way to return modified arguments when using overloading.

Reproduce code:
---------------
class Foo {
    function __call($method, &$args)
    {
        print $args[0]."\n";
        $args[0] = 5;
        print $args[0]."\n";
        return true;
    }
}

$v = 'str';

$o = new Foo();
$o->test($v);

var_dump($v);


Expected result:
----------------
str
5
int(5)


Actual result:
--------------
str
5
string(3) "str"


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-02 17:51 UTC] pollita@php.net
Summary from IRC:

This should be fixable by selectively populating arg_info in zend_std_get_method() with a structure that turns on the pass rest by ref flag.   That'll tell the macros in zend_vm_def.h to send the arguments by reference.  From there, you might need to modify zend_std_call_user_call() a little bit where it's building the args array... (Havn't looked close enough to be sure)

While addressing this, you should look at the return value as well, again this should be a minor matter of checking the __call implementation and flipping the return type in zend_std_get_method()...
 [2007-08-31 12:13 UTC] tony2001@php.net
<?php 
class Foo {
    function __call($method, $args) //NB 1
    {
        print $args[0]."\n";
        $args[0] = 5;
        print $args[0]."\n";
        return true;
    }
}

$v = 'str';

$o = new Foo();
$o->test(&$v); //NB 2 

var_dump($v);
?>

This code results in:
str
5
int(5)

So I believe this report can be closed.
 [2007-09-04 19:59 UTC] andrei@php.net
You're forcing a call-time reference. How is this going to help when call-time reference stuff is deprecated?
 [2007-09-07 21:07 UTC] tony2001@php.net
I don't see how it is possible without forcing the reference OR having a method like __callByRef(), which makes little sense to me.
 [2007-09-15 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2011-04-09 17:15 UTC] dan dot lugg at gmail dot com
Has this issue received any attention? I've noticed it cropping up elsewhere in the bug database. An example of where it is failing still:

    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);
    echo $test;

----

Expected result:
    string(6) "foobar"

----

Actual result:
    Warning: Parameter 1 to testFunction() expected to be a reference, value given in ...
    string(3) "foo"

----

By passing the argument by reference at call time, it succeeds:

    TestClass::testFunction(&$test);

Result:
    string(6) "foobar"

However, this of course begins issuing deprecation warnings in my IDE regarding the call-time passing-by-reference.

----

Any progress?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 02:01:30 2024 UTC