php/mongodb: how does references work in php?

Posted by harald on Stack Overflow See other posts from Stack Overflow or by harald
Published on 2010-06-08T19:01:27Z Indexed on 2010/06/08 19:12 UTC
Read the original article Hit count: 412

Filed under:
|
|

hello,

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

the question was:

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

var_dump($ref); 

$ref does not contain '_id', because it's a reference to $b, the handbook states. (the code snippet is taken from the php mongo documentation)

i should add, that:

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

var_dump($ref); 

in this case $ref contains the _id -- for those, who do not know, what the insert method of mongodb-php-driver does -- because $ref is passed by reference (note the $b with and without referencing '&').

on the other hand ...

function test(&$data) { 
    $data['_id'] = time(); 
} 

$b   = array('x' => 1); 
$ref =& $b; 

test($ref);

var_dump($ref); 

$ref contains _id, when i call a userland function.

my question is: how does the references in these cases differ? my question is probably not mongodb specific -- i thought i would know how references in php work, but apparently i do not: the answer in the mongodb user-group was, that this was the way, how references in php work. so ... how do they work -- explained with these two code-snippets?

thanks in advance!!!

© Stack Overflow or respective owner

Related posts about php

Related posts about references