Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing __call() parameters by reference fails. Any work around?
    primarykey
    data
    text
    <p>I have written a fairly simple lazy loading proxy class, which I have documented in the past over at <a href="http://blog.simonholywell.com/post/2072272471/logging-global-php-objects-lazy-loading-proxy" rel="nofollow">http://blog.simonholywell.com/post/2072272471/logging-global-php-objects-lazy-loading-proxy</a></p> <p>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:</p> <blockquote> <p>Fatal error: Method LazyLoader::__call() cannot take arguments by reference in /home/simon/file/name.php</p> </blockquote> <p>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.</p> <p>The lazy loading proxy class looks like this, but the description in <a href="http://blog.simonholywell.com/post/2072272471/logging-global-php-objects-lazy-loading-proxy" rel="nofollow">my blog post</a> explains the purpose better:</p> <pre><code>&lt;?php /** * @author Simon Holywell &lt;treffynnon@php.net&gt; */ 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-&gt;setClassName($class_name); $this-&gt;setClassPath($class_path); } public function setClassName($class_name) { if(null !== $class_name) { $this-&gt;class_name = $class_name; } } public function getClassName() { return $this-&gt;class_name; } public function setClassPath($class_path) { if(null !== $class_path) { $this-&gt;class_path = $class_path; } } public function getClassPath() { return $this-&gt;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-&gt;instance) { $this-&gt;instance = $this-&gt;initInstance(); } return $this-&gt;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-&gt;class_path); $class_name = $this-&gt;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, &amp;$arguments) { $instance = $this-&gt;getInstance(); Logger::log('Called: ' . $this-&gt;class_name . '-&gt;' . $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-&gt;class_name . '-&gt;' . $name); return $this-&gt;getInstance()-&gt;$name; } public function __set($name, $value) { Logger::log('Setting property: ' . $this-&gt;class_name . '-&gt;' . $name); $this-&gt;getInstance()-&gt;$name = $value; } public function __isset($name) { Logger::log('Checking isset for property: ' . $this-&gt;class_name . '-&gt;' . $name); return isset($this-&gt;getInstance()-&gt;$name); } public function __unset($name) { Logger::log('Unsetting property: ' . $this-&gt;class_name . '-&gt;' . $name); unset($this-&gt;getInstance()-&gt;$name); } } </code></pre> <p>Any help greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload