php / mongodb:引用如何在php中运行?

时间:2022-08-23 08:05:56

i asked this in the mongodb user-group, but was not satisfied with the answer, so -- maybe someone at * can enlighten me:

我在mongodb用户组中问过这个问题,但对答案不满意,所以 - 也许有人在*上可以启发我:

EDIT:

i've re-written my question, because apparently it wasn't clear, what was happening -- please try my test-code before answering. thanks!

我重新写了我的问题,因为显然它不清楚,发生了什么 - 请在回答之前尝试我的测试代码。谢谢!

<?php

// test: a
$data = array('x' => 1);

function a(&$data) {
    $m = new mongo();
    $c = $m->selectDB('test')->selectCollection('test');

    $c->insert($data);
}

a($data);
print_r($data);

// test: b
$data = array('x' => 1);

function b($data) {
    $m = new mongo();
    $c = $m->selectDB('test')->selectCollection('test');

    $c->insert($data);
}

b($data);
print_r($data);

// test: c
$data = array('x' => 1);

function c(&$data) {
    $data['_id'] = new MongoId();
}

c($data);
print_r($data);

// test: d
$data = array('x' => 1);

function d($data) {
    $data['_id'] = new MongoId();
}

d($data);
print_r($data);

?>

output:

Array
(
    [x] => 1
)

Array
(
    [x] => 1
    [_id] => MongoId Object
        (
        )

)

Array
(
    [x] => 1
    [_id] => MongoId Object
        (
        )

)

Array
(
    [x] => 1
)

my question: why does pass-by-reference apparently work different for mongo insert compared to my plain php function call?

我的问题:与我的普通php函数调用相比,为什么pass-by-reference显然与mongo insert不同?

thanks!

2 个解决方案

#1


1  

Kristina Chodorow, the maintainer of the mongoDB php extension, wrote up a nice article in her blog about this issue:

mongoDB php扩展的维护者Kristina Chodorow在她的博客中写了一篇关于这个问题的好文章:

http://www.snailinaturtleneck.com/blog/2011/09/07/more-php-internals-references/

in my opinion it clarifies how things work in php, i still think that references in php are really awkward ...

在我看来,它澄清了PHP中的工作原理,我仍然认为php中的引用真的很尴尬......

#2


0  

When you have something like $ref = &$someVar. $ref now refers to the value at $someVar.

当你有像$ ref =&$ someVar这样的东西时。 $ ref现在引用$ someVar的值。

EDIT:

The MongoDB Manual at PHP.net says:

PHP.net上的MongoDB手册说:

Example #1 MongoCollection::insert() _id example Inserting an object will add an _id field to it, unless it is passed by reference.

示例#1 MongoCollection :: insert()_id示例插入对象将向其添加_id字段,除非它通过引用传递。

<?php

$a = array('x' => 1);
$collection->insert($a);
var_dump($a)

$b = array('x' => 1);
$ref = &$b;
$collection->insert($ref);
var_dump($ref);

?>

#1


1  

Kristina Chodorow, the maintainer of the mongoDB php extension, wrote up a nice article in her blog about this issue:

mongoDB php扩展的维护者Kristina Chodorow在她的博客中写了一篇关于这个问题的好文章:

http://www.snailinaturtleneck.com/blog/2011/09/07/more-php-internals-references/

in my opinion it clarifies how things work in php, i still think that references in php are really awkward ...

在我看来,它澄清了PHP中的工作原理,我仍然认为php中的引用真的很尴尬......

#2


0  

When you have something like $ref = &$someVar. $ref now refers to the value at $someVar.

当你有像$ ref =&$ someVar这样的东西时。 $ ref现在引用$ someVar的值。

EDIT:

The MongoDB Manual at PHP.net says:

PHP.net上的MongoDB手册说:

Example #1 MongoCollection::insert() _id example Inserting an object will add an _id field to it, unless it is passed by reference.

示例#1 MongoCollection :: insert()_id示例插入对象将向其添加_id字段,除非它通过引用传递。

<?php

$a = array('x' => 1);
$collection->insert($a);
var_dump($a)

$b = array('x' => 1);
$ref = &$b;
$collection->insert($ref);
var_dump($ref);

?>