Since the release of PHP5, the object model has changed. While it 'fixes' a lot of 'wrongs' I still get confused as to the how it should be used and what the result should be.
My biggest gripe is that the 'where should references be defined' part is not enforced.
By example:
// use the '&' reference thingy in the function arguments
function foo(&$var) {
$var = 2;
}
// function doesn't care what the arg is, reference of not.
function bar($var) {
$var = 2;
}
$var = 1;
foo($var);
// overwrite $var;
$var = 1;
bar(&$var); // declare $var a reference in the function call.
This confuses me. Which of these implementations is preferable over the other? Should I just pick one and 'not worry be happy'? Why isn't one of these enforced.
I would say that the second implementation is the 'prefered way', because it doesn't force the reference on you, and leaves the choice of $var being a reference to the person using the function.
Even more confusing is the fact that the following also works without a hitch:
function foo($var) {
$var = 2;
}
$int = 1;
$var =& $int;
foo(&$var); // passing a reference to a reference.
print $var;
// prints 2, wuh?
I know the manual states:
They are not like C pointers; instead, they are symbol table aliases.
But its still confusing nonetheless. In my opinion E_STRICT should give me a notice (at least) to inform me of my questionable behaviour. But it doesn't, it just ignores me and goes on its merry way.