php - Passing __call() parameters by reference fails. Any work around? -
i have written simple lazy loading proxy class, have documented in past on @ http://blog.simonholywell.com/post/2072272471/logging-global-php-objects-lazy-loading-proxy
now convert project work have been tripped proxying method, has 1 of parameters passed reference. when goes through __call method of proxy class get:
fatal error: method lazyloader::__call() cannot take arguments reference in /home/simon/file/name.php
any clever ideas of how might solved or worked around. preferably without refactoring code requires pass reference if possible.
the lazy loading proxy class looks this, description in my blog post explains purpose better:
<?php /** * @author simon holywell <treffynnon@php.net> */ class lazyloadingproxy { /** * instance of actual class stored. * @var $instance object */ private $instance = null; /** * name of class load * @var $class_name string */ private $class_name = null; /** * path class load * @var $class_path string */ private $class_path = null; /** * set name of class lazyloader should proxy * @ 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; } /** * instance of class lazyloader proxying. * if instance not exist initialised. * @return object instance of class lazyloader proxying */ public function getinstance() { if(null === $this->instance) { $this->instance = $this->initinstance(); } return $this->instance; } /** * load instance of class being proxied. * @return object instance of class lazyloader 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 call functions on class being proxied. * @return mixed whatever requested method 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 standard php magic methods access * class properties of class 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 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 appreciated.
the short answer not pass reference. in 99.9% of cases, don't need it. , in other 0.1% can work around lack of references anyway. remember, objects passed object-reference anyway don't need use variable references them.
now, far workaround, i'd hardcode adapter. extend proxy particular class, , include wrapper particular method. instantiate new extended class instead of core proxy class. dirty? absolutely. it's workaround without refactoring original class not take argument reference, or refactoring caller prevent passing reference (which deprecated anyway).
Comments
Post a Comment