Req #71256 [NEW]: Magic __call() method should provide references as arguments.

From: Date: Thu, 31 Dec 2015 15:25:04 +0000
Subject: Req #71256 [NEW]: Magic __call() method should provide references as arguments.
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-198337@lists.php.net to get a copy of this message
From:             toolrulez at gmail dot com
Operating system: all
PHP version:      Irrelevant
Package:          Class/Object related
Bug Type:         Feature/Change Request
Bug description:Magic __call() method should provide references as arguments.

Description:
------------
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:
---------------
<?php

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();
$object->doSomethingTo($data);

var_dump($data);

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
int(42)

-- 
Edit bug report at https://bugs.php.net/bug.php?id=71256&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=71256&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=71256&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=71256&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=71256&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=71256&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=71256&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=71256&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=71256&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=71256&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=71256&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=71256&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=71256&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=71256&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71256&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=71256&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=71256&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=71256&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71256&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=71256&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=71256&r=mysqlcfg



Thread (3 messages)

« previous php.bugs (#198337) next »