Passing __call() parameters by reference fails. Any work around?
I have written a fairly simple lazy loading proxy class, which I have documented in the past over at http://blog.simonholywell.com/post/2072272471/logging-global-php-objects-lazy-loading-proxy
Now as I convert another project to work with it I have been tripped up by proxying a method, which has one of its parameters passed to it by reference. When this goes through the __call method of my proxy class I get:
Fatal error: Method LazyLoader::__call() cannot take arguments by reference in /home/simon/file/name.php
Any clever ideas of how this might be solved or worked around. Preferably without refactoring the code that requires the pass by reference if possible.
The lazy loading proxy class looks like this, but the description in my blog post explains the purpose better:
<?php
/**
* @author Simon Holywell <treffynnon@php.net>
*/
class LazyLoadingProxy {
/**
* Where the instance of the actual class is stored.
* @var $instance object
*/
private $instance = null;
/**
* The name of the class to load
* @var $class_name string
*/
private $class_name = null;
/**
* The path to the class to load
* @var $class_path string
*/
private $class_path = null;
/**
* Set the name of the class this LazyLoader should proxy
* at the time of instantiation
* @param $class_name string
*/
public function __construct($class_name, $class_path = null) {
$this->setClassName($class_name);
$this->setClassPath($class_path);
}
public function setClassName($class_name) {
if(null !== $class_name) {
$this->class_name = $class_name;
}
}
public function getClassName() {
return $this->class_name;
}
public function setClassPath($class_path) {
if(null !== $class_path) {
$this->class_path = $class_path;
}
}
public function getClassPath() {
return $this->class_path;
}
/**
* Get the instance of the class this LazyLoader is proxying.
* If the instance does not already exist then it is initialised.
* @return object An instance of the class this LazyLoader is proxying
*/
public function getInstance() {
if(null === $this->instance) {
$this->instance = $this->initInstance();
}
return $this->instance;
}
/**
* Load an instance of the class that is being proxied.
* @return object An instance of the class this LazyLoader is proxying
*/
private function initInstance() {
Logger::log('Loaded: ' . $class_name);
require_once($this->class_path);
$class_name = $this->class_name;
return new $class_name();
}
/**
* Magic Method to call functions on the class that is being proxied.
* @return mixed Whatever the requested method would normally return
*/
public function __call($name, &$arguments) {
$instance = $this->getInstance();
Logger::log('Called: ' . $this->class_name . '->' . $name . '(' . print_r($arguments, true) . ');');
return call_user_func_array(
array($instance, $name),
$arguments
开发者_如何学编程 );
}
/**
* These are the standard PHP Magic Methods to access
* the class properties of the class that is being proxied.
*/
public function __get($name) {
Logger::log('Getting property: ' . $this->class_name . '->' . $name);
return $this->getInstance()->$name;
}
public function __set($name, $value) {
Logger::log('Setting property: ' . $this->class_name . '->' . $name);
$this->getInstance()->$name = $value;
}
public function __isset($name) {
Logger::log('Checking isset for property: ' . $this->class_name . '->' . $name);
return isset($this->getInstance()->$name);
}
public function __unset($name) {
Logger::log('Unsetting property: ' . $this->class_name . '->' . $name);
unset($this->getInstance()->$name);
}
}
Any help greatly appreciated.
The short answer is to not pass by reference. In 99.9% of cases, you don't need it. And in those other 0.1% you can work around the lack of references anyway. Remember, objects are passed by object-reference anyway so you don't need to use variable references for them.
Now, as far as a workaround, I'd personally hardcode that into an adapter. So extend your proxy for that particular class, and include a wrapper for that particular method. Then instantiate that new extended class instead of the core proxy for that class. Is it dirty? Absolutely. But it's your only workaround without refactoring the original class to not take the argument by reference, or refactoring the caller to prevent passing by reference (which is deprecated anyway).
Here is a trick for you, to pass variables by references via __call magic method: I don't now how long will this works or in witch php versions. I tested on php 5.3.2
Fitst you cannot pass $arguments variable by $reference in __call function definition. Because php falls with error.
So first: Here is a code what does not do, what you want with referenced arguments, but nearly good ;)
class A { public $x = array(); }
class Teszt{
private function addElement( &$list ){
$list->x[] = 'new element';
return count($list->x);
}
public function __call($name,$arguments){
if (!preg_match('#^add([0-9]+)Element$#', $name, $matches) || $matches[1]<1){
trigger_error ("Function is not exists teszt::$name", E_USER_ERROR);
}
$ret = null;
for($i=0;$i<$matches[1];$i++){
$ret = call_user_func_array(array($this,'addElement'), $arguments);
}
return $ret;
}
}
$a = new A();
$a->x = array('old element','old element');
$t = new Teszt();
$cnt = $t->add5Element($a);
var_dump($a);
var_dump($cnt);
OUTPUT:
PHP Warning: Parameter 1 to Teszt::addElement() expected to be a reference, value given in /home/gkovacs/callMagicWithReference.php on line 19
PHP Stack trace:
PHP 1. {main}() /home/gkovacs/callMagicWithReference.php:0
PHP 2. Teszt->add2Element() /home/gkovacs/callMagicWithReference.php:27
PHP 3. Teszt->__call() /home/gkovacs/callMagicWithReference.php:0
PHP 4. call_user_func_array() /home/gkovacs/callMagicWithReference.php:19
PHP Warning: Parameter 1 to Teszt::addElement() expected to be a reference, value given in /home/gkovacs/callMagicWithReference.php on line 19
PHP Stack trace:
PHP 1. {main}() /home/gkovacs/callMagicWithReference.php:0
PHP 2. Teszt->add2Element() /home/gkovacs/callMagicWithReference.php:27
PHP 3. Teszt->__call() /home/gkovacs/callMagicWithReference.php:0
PHP 4. call_user_func_array() /home/gkovacs/callMagicWithReference.php:19
object(A)#1 (1) {
["x"]=>
array(2) {
[0]=>
string(11) "old element"
[1]=>
string(11) "old element"
}
}
NULL
ohhh :((( After a litle 'HACK':
class A { public $x = array(); }
class Teszt{
private function addElement( &$list ){
$list->x[] = 'new element';
return count($list->x);
}
public function __call($name,$arguments){
if (!preg_match('#^add([0-9]+)Element$#', $name, $matches) || $matches[1]<1){
trigger_error ("Function is not exists teszt::$name", E_USER_ERROR);
}
$ret = null;
//Look at my hand, because i will cheat
foreach($arguments as $k=>&$v){ }
//end of cheat
for($i=0;$i<$matches[1];$i++){
$ret = call_user_func_array(array($this,'addElement'), $arguments);
}
return $ret;
}
}
$a = new A();
$a->x = array('old element','old element');
$t = new Teszt();
$cnt = $t->add5Element($a);
var_dump($a);
var_dump($cnt);
Output:
object(A)#1 (1) {
["x"]=>
array(4) {
[0]=>
string(11) "old element"
[1]=>
string(11) "old element"
[2]=>
string(11) "new element"
[3]=>
string(11) "new element"
}
}
int(4)
As we wanted. This works only with objects, but not with array-s. There is a way with array-s to call-time-pass-by-reference ... like this: (and of corse you can use this way with objects too)
$cnt = $t->add5Element(&$a);
In this case php generate notices ...
The easiest way to redefine the function if it is possible. in my code , : private functionaddElement($list) , don't define as reference in parameter list. And as you read in previous message , it will works beacause objects are passed by reference automatically But sometimes , you can not redefine functions because of implemented interfaces or other reasons ...
Its can. So is hungry.
<?php
class MyClass(){
public function __call($name, $params)
{
var_dump($params);
$params[0][0] = '222';
}
}
$obj = new MyClass();
$info = 3;
var_dump($info);
$obj->setter([&$info]);
var_dump($info);
精彩评论