The PHP doc explains how to use and why, functions that return links.
In your code, the getObject() function also needs & (and a call), otherwise the link will be lost, and the data, when used, will be based on PHP copy-on-write (the returned data and the original data indicate both the same actual data until there is no change in one of them => two data blocks with a distinct service life)
This will not work (syntax error)
$a = array(1, 2, 3); return &$a;
this does not work as intended (the link is not returned)
$a = array(1, 2, 3); $ref = &$a; return $ref;
and without adding & to the function call, as you said, the link did not return.
To the question of why ... It seems not a consistent answer.
- If one of
& missing, PHP processes the data as if it was not a reference (e.g. returning an array, for example) without any warnings - here's some weirdness with link return functions
PHP has evolved over the years, but still inherits some of the first bad design options. This seems to be one of them (this syntax is error prone, as you can easily skip one & ... and no warning ahead ..., and also why not directly return the link, for example return &$var; ;?) . PHP has made some progress, but still , traces of poor design exist.
You may also be interested in this chapter of the document linked above.
Do not use return-by-reference to improve performance. The engine automatically optimizes this on its own. Return links only when you have the right technical reason.
Finally, it is best not to look too much at the equivalence between pointers to C and PHP references (Perl is closer to PHP in this regard). PHP adds a layer between the actual pointer to the data and the variables, and links point to this layer, not the actual data. But the link is not a pointer. If $a is an array and $b is a reference to $a , using $a or $b to access the array is equivalent. The dereferencing syntax does not exist, and *$b , for example, in C. $b , should be considered an alias of $a . This is also the reason that a function can only return a reference to a variable.