gpt4 book ai didi

php - 通过引用传递 __call() 参数失败。有解决办法吗?

转载 作者:行者123 更新时间:2023-12-02 01:20:36 30 4
gpt4 key购买 nike

我编写了一个相当简单的延迟加载代理类,我过去曾在 http://blog.simonholywell.com/post/2072272471/logging-global-php-objects-lazy-loading-proxy 记录过该类。

现在,当我转换另一个项目以使用它时,我被代理一个方法绊倒了,该方法的参数之一通过引用传递给它。当这通过我的代理类的 __call 方法时,我得到:

Fatal error: Method LazyLoader::__call() cannot take arguments by reference in /home/simon/file/name.php

关于如何解决或解决这个问题的任何聪明的想法。如果可能的话,最好不要重构需要通过引用传递的代码。

延迟加载代理类看起来是这样的,但是my blog post中的描述更好地解释目的:

<?php
/**
* @author Simon Holywell <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9befe9fefdfde2f5f5f4f5dbebf3ebb5f5feef" rel="noreferrer noopener nofollow">[email protected]</a>>
*/
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);
}
}

非常感谢任何帮助。

最佳答案

简短的回答是不要通过引用传递。在 99.9% 的情况下,您不需要它。无论如何,在另外 0.1% 中,您可以解决缺乏引用文献的问题。请记住,objects are passed by object-reference anyway因此您不需要为它们使用变量引用。

现在,就解决方法而言,我个人会将其硬编码到适配器中。因此,扩展该特定类的代理,并包含该特定方法的包装器。然后实例化该新的扩展类而不是该类的核心代理。脏吗?绝对地。但这是您唯一的解决方法,无需重构原始类以不通过引用获取参数,或重构调用者以防止通过引用传递(无论如何,这已被弃用)。

关于php - 通过引用传递 __call() 参数失败。有解决办法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5043575/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com