|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #71256 Magic __call() method should provide references as arguments.
Submitted: 2015-12-31 15:25 UTC Modified: 2020-01-08 16:10 UTC
Avg. Score:4.4 ± 0.8
Reproduced:12 of 14 (85.7%)
Same Version:11 (91.7%)
Same OS:11 (91.7%)
From: toolrulez at gmail dot com Assigned:
Status: Wont fix Package: Class/Object related
PHP Version: Irrelevant OS: all
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2015-12-31 15:25 UTC] toolrulez at gmail dot com
In PHP 7 (and all previous versions),
there is no elegant way to make the __call magic method work properly with methods that use parameters by reference.
In php 5.4, the directive "Call-time pass-by-reference" was removed, which is a good thing since it wasn't very clean.
But it provided a way to make __call arguments work with pass-by-reference functions.

So, in order to make things work more intuitively, I suggest that __call should provide an array of arguments references instead of an array of copies.
With this, it would be possible to modify the contents of a variable passed as a parameter through the __call magic method.

--- Notes 

Please note that I'm aware of the fact that objects are always passed by reference and that, if an object was passed as $data in the example below, things would work as expected.
But abjects are not always the most effective way to go (and that it is not always possible to use them when the __call method is used as a pattern to refactor code).

Also, please note that there is a trick to make this "work" as expected. The trick would be to pass an array of explicit references to the method doSomthing and then process the array inside the __call method to give the right array to call_user_func_array.
But this technique would require you to know, from the outside of class A, that this specific __call method will be used and that it works exacltly like described, which is not the appropriate way to use objects (i.e. as a blackbox doing things internally that we -the users of that object- are not necessarily interested in or aware of).

Test script:

Class A
	public function __call($method, $args)
		call_user_func_array(array($this, $method), $args);

	// Pass by reference here.
	// we expect data to change outside this scope.
	protected function doSomethingTo(&$data)
		$data = "done";

$data = 42;

$object = new A();


Expected result:
string(4) "done"

Actual result:
Warning: Parameter 1 to A::doSomethingTo() expected to be a reference, value given in /in/GhZii on line 7


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-02 08:59 UTC] toolrulez at gmail dot com
...And happy new year to you all. :)
 [2020-01-08 16:10 UTC]
-Status: Open +Status: Wont fix
 [2020-01-08 16:10 UTC]
I'll go ahead and put this feature request out of it's misery by saying that we will not implement this.

The fundamental problem is that we need to decided whether an argument needs to be passed by-value or by-reference when the call is made, and we just don't have the information at that point.

Always passing by-reference is not an option, because it has multiple very undesirable side-effects: First, it will not generate a warning when passing an undefined variable. Second, it will initialize the variable (or array offset or object property etc) to null if it isn't initialized already. And third, it will create a reference structure allocation, which has a significant performance and memory usage impact.

As always, I recommend to avoid the use of references.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Oct 23 20:03:34 2021 UTC