使用反射通过引用传递参数
Passing an argument by reference with reflection
This article有以下方法:
/**
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
*/
public function invokeMethod(&$object, $methodName, array $parameters = array())
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $parameters);
}
我的问题是...在函数声明中 $object
前面有一个 & 符号是否有特殊原因?通常这意味着您正在通过引用传递,但 PHP 默认情况下不通过引用传递对象?
如您在 PHP 文档的 Function arguments 部分中所见:
PHP supports passing arguments by value (the default), passing by reference, and default argument values. Variable-length argument lists are also supported.
默认情况下,参数按值传递。
至于对象,它们似乎是通过引用传递的,但事实并非如此。请参阅 Objects and references 其中指出:
A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.
因此,为了避免混淆,我总是假设参数不是按值传递的,即使对于对象也是如此。如果您希望通过引用传递它,请添加 &
以确保您确实通过引用传递它。
这是一个对象示例:
<?php
// Passed by value... won't be affected
function byValue($arg) {
$arg = null;
}
// Passed by reference... will be affected
function byReference(&$arg) {
$arg = null;
}
$obj = new StdClass;
var_dump($obj); // Untouched object created
byValue($obj);
var_dump($obj); // After 'trying' to set it to null
byReference($obj);
var_dump($obj); // After setting it to null for real
My question is... is there a particular reason why $object has an
ampersand before it in the function declaration?
在这段代码中,没有。 $object
仅用于此函数内部的值。使用它的唯一两个地方是传递给 get_class()
(它按值获取其参数),并传递给 ReflectionMethod::invokeArgs()
(它也按值获取其参数)。
因此,没有必要参考 $object
。
This article有以下方法:
/**
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
*/
public function invokeMethod(&$object, $methodName, array $parameters = array())
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $parameters);
}
我的问题是...在函数声明中 $object
前面有一个 & 符号是否有特殊原因?通常这意味着您正在通过引用传递,但 PHP 默认情况下不通过引用传递对象?
如您在 PHP 文档的 Function arguments 部分中所见:
PHP supports passing arguments by value (the default), passing by reference, and default argument values. Variable-length argument lists are also supported.
默认情况下,参数按值传递。
至于对象,它们似乎是通过引用传递的,但事实并非如此。请参阅 Objects and references 其中指出:
A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.
因此,为了避免混淆,我总是假设参数不是按值传递的,即使对于对象也是如此。如果您希望通过引用传递它,请添加 &
以确保您确实通过引用传递它。
这是一个对象示例:
<?php
// Passed by value... won't be affected
function byValue($arg) {
$arg = null;
}
// Passed by reference... will be affected
function byReference(&$arg) {
$arg = null;
}
$obj = new StdClass;
var_dump($obj); // Untouched object created
byValue($obj);
var_dump($obj); // After 'trying' to set it to null
byReference($obj);
var_dump($obj); // After setting it to null for real
My question is... is there a particular reason why $object has an ampersand before it in the function declaration?
在这段代码中,没有。 $object
仅用于此函数内部的值。使用它的唯一两个地方是传递给 get_class()
(它按值获取其参数),并传递给 ReflectionMethod::invokeArgs()
(它也按值获取其参数)。
因此,没有必要参考 $object
。