- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我一直在阅读关于 SO 和其他地方的文章,但是我似乎找不到任何结论。
有没有什么方法可以有效地通过这个调用堆栈传递引用,从而产生下面示例中描述的所需功能?虽然这个例子并没有试图解决它,但它确实说明了问题:
class TestClass{
// surely __call would result similarly
public static function __callStatic($function, $arguments){
return call_user_func_array($function, $arguments);
}
}
// note argument by reference
function testFunction(&$arg){
$arg .= 'bar';
}
$test = 'foo';
TestClass::testFunction($test);
// expecting: 'foobar'
// getting: 'foo' and a warning about the reference
echo $test;
仅关注 call_user_func_array()
,我们可以确定(至少对于 PHP 5.3.1)您不能通过引用隐式传递参数:
function testFunction(&$arg){
$arg .= 'bar';
}
$test = 'foo';
call_user_func_array('testFunction', array($test));
var_dump($test);
// string(3) "foo" and a warning about the non-reference parameter
通过显式传递数组元素 $test
作为引用,我们可以缓解这种情况:
call_user_func_array('testFunction', array(&$test));
var_dump($test);
// string(6) "foobar"
当我们使用 __callStatic()
引入类时,通过引用显式调用时间参数似乎如我预期的那样执行,但发出弃用警告(在我的 IDE 中):
class TestClass{
public static function __callStatic($function, $arguments){
return call_user_func_array($function, $arguments);
}
}
function testFunction(&$arg){
$arg .= 'bar';
}
$test = 'foo';
TestClass::testFunction(&$test);
var_dump($test);
// string(6) "foobar"
TestClass::testFunction()
中省略引用运算符导致 $test
按值传递给 __callStatic()
,并且当然是通过 call_user_func_array()
按值作为数组元素传递给 testFunction()
。这会导致警告,因为 testFunction()
需要引用。
四处寻找,一些额外的细节浮出水面。 __callStatic()
定义,如果编写为通过引用返回(public static function &__callStatic()
)没有可见的效果。此外,将 __callStatic()
中的 $arguments
数组的元素重铸为引用,我们可以看到 call_user_func_array()
有点像预期的那样工作:
class TestClass{
public static function __callStatic($function, $arguments){
foreach($arguments as &$arg){}
call_user_func_array($function, $arguments);
var_dump($arguments);
// array(1) {
// [0]=>
// &string(6) "foobar"
// }
}
}
function testFunction(&$arg){
$arg .= 'bar';
}
$test = 'foo';
TestClass::testFunction($test);
var_dump($test);
// string(3) "foo"
这些结果是预期的,因为 $test
不再通过引用传递,更改不会传回它的范围。然而,这证实了 call_user_func_array()
实际上按预期工作,并且问题肯定仅限于调用魔法。
进一步阅读后,它似乎是 PHP 处理用户函数的“错误”,__call()
/__callStatic()
魔法.我仔细阅读了现有或相关问题的错误数据库,并找到了一个,但无法再次找到它。我正在考虑发布另一份报告,或请求重新打开现有报告。
最佳答案
这很有趣。
TestClass::testFunction(&$string);
这有效,但它也会抛出一个调用时间引用传递警告。
主要问题是 __callStatic
的第二个参数按值传入。它正在创建数据的副本,除非该数据已经是一个引用。
我们可以这样复制调用错误:
call_user_func_array('testFunction', array( $string ));
// PHP Warning: Parameter 1 to testFunction() expected to be a reference, value given
call_user_func_array('testFunction', array( &$string ));
echo $string;
// 'helloworld'
我尝试修改 __callStatic
方法以通过引用深度复制数组,但这也不起作用。
我很确定由 __callStatic
引起的立即复制将成为这里的 killer ,如果没有足够的跳圈,你将无法做到这一点贴纸,语法方面。
关于php - __callStatic()、call_user_func_array()、引用资料和 PHP 5.3.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5602616/
第一次尝试处理这个 call_user_func_array,但有些东西不起作用,因为我没有收到函数的响应,我会遗漏什么? function _a_($id, $text) {
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
在组合数组时遇到了一些问题......这可行: $un[0] = array("0:0:0"); //$un[1] = array("1:1:1"); $flat = call
当函数包含引用参数时,下面的函数会产生错误,例如: function test(&$arg, &$arg2) { // some code } 现在我不能对上面的函数使用call_user_fun
我正在使用 mysqli 和准备好的语句为我的数据库编写一个简单的搜索引擎,我目前正在使用 call_user_func_array 来动态创建我的查询。我的问题是:使用 mysqli 是否有更快或更
我在 stack 上阅读了其他答案关于使用 call_user_func_array 与仅调用函数有关,但我仍然无法收集何时应该使用前者。我知道当您不知道传递了多少个参数时,您可能想使用 call_u
本周早些时候我看到了一段代码(不幸的是,我无法检索),我很好奇作者是如何实现 __call() 魔法方法的。代码如下所示: class Sample { protected function
我目前正在构建一个数据库处理程序。在 Firebird 数据库之上运行 PHP 5.4.16。 当我创建了准备好的语句后,我需要使用参数执行 SQL。我做了这个功能: private function
这个问题在这里已经有了答案: How to call the constructor with call_user_func_array in PHP (1 个回答) 关闭7年前。 我已经搜索了许多
这个问题在这里已经有了答案: How to call the constructor with call_user_func_array in PHP (1 个回答) 关闭7年前。 我已经搜索了许多
我使用的是 PHP 5.4.25。采取以下代码: 'John', 'last_name'=>'Doe']); call_user_func_array('fails', ['first_name'=>
这个问题已经有答案了: Sort array using array_multisort() with dynamic number of arguments/parameters/rules/dat
目前我正在使用: call_user_func( $func, $_POST['a'],$_POST['b'],$_POST['c'],$_POST['d'],$_POST['
我正在构建一个模板系统,但遇到了动态调用函数的问题。 当我尝试以下操作时: $args = array( 4, 'test' => 'hello', 'hi' ); 你知道..
PHP 的 call_user_func_array 在 Python 中是否有等效项? ? 最佳答案 以*开头的数组调用函数: function(*array) 关于Python 中的 PHP ca
我今天遇到了一个有趣的问题。我们有一个利用 Zend Frameworks 缓存功能的应用程序。对此应用程序的请求通常使用以下行调用工厂方法 $result = call_user_func_arr
这个问题在这里已经有了答案: Build SELECT query with dynamic number of LIKE conditions as a mysqli prepared state
我有一个方法,简化后看起来像这样: class Foo { public function bar($id) { // do stuff using $this, error occ
如何记录 call_user_func_array 中发生的 fatal error ?它位于作为守护进程运行的 CLI 脚本中。我想在发生错误时记录日志,但它总是抛回错误消息。 我尝试过但无法实现的
当使用 call_user_func_array() 我想通过引用传递一个参数。我该怎么做。例如 function toBeCalled( &$parameter ) { //...Do So
我是一名优秀的程序员,十分优秀!