【发布时间】:2012-10-12 11:13:55
【问题描述】:
总结
我正在尝试找到一种方法将引用变量向上更改两级,同时避免Deprecated: Call-time pass-by-reference has been deprecated
我所做的研究
我查看了this 和this,似乎call_user_func_array 可以使警告静音,但我认为我遗漏了一些东西。
问题
我正在使用 MongoDB 和 PHP,以下方法属于一个模型,并在保存之前简单地检查通过引用传递给它的输入的架构。
// $this->collection is the MongoCollection object
public function save(&$entry) {
if( empty($entry) ) return false;
if( !$this->checkSchema($entry) ) $this->throwDbError('Schema violation in `' . get_class($this) . '`');
try { return $this->collection->save(&$entry); } // <---- want to avoid using &
catch (Exception $e) { return $this->throwDbError($e); }
}
MongoCollection::save ($this->collection->save) 将使用新的文档 ID 将 _id 字段附加到 $entry 上。但是,此更改并未反映在传递给上述方法的$entry 上除非我通过引用传递它的调用时间。 (本质上我希望MongoCollection::save 能够修改$entry 两级)
好的,这是我解释问题的最佳方法,如果您需要澄清,请告诉我。
【问题讨论】:
-
"// & 符号之前,然后按“删除”。说真的,在 php5 中它没有任何意义。您只能在函数声明中指定该变量仅作为引用传递。
-
我知道我可以,但是传递给上述方法的 $entry 没有附加 [_id] (我需要)
-
@NathanKot 似乎,
$entry应该是一个对象。要么,要么记住,方法可以有一个返回值;) -
@Nathan Kot:好吧,那只是一个 mongodb 驱动程序错误
-
@KingCrunch:是的,我也是这么想的,但是没有 - php.net/manual/en/mongocollection.save.php
标签: php mongodb pass-by-reference