If the resource exists it is because there is a reason for its use. Unless there is something complementary that makes it obsolete.
Probably the recommendation not to use is to try optimizations. This can be considered premature optimization, mainly because PHP already does optimization of passing the argument by reference when this is useful even if it has no mention to pass by reference. Of course, if the expected semantics is not the passage by reference, the optimization is about using the technique of copy-on-write. So an attempt to optimize by passing something by reference can even hinder this optimization.
Nor will I repeat what I always say that micro-optimizing PHP is not usually the most productive thing.
Although rare, there may be the problem of starting to have references p/references and losing control if it is used in exaggeration.
But if the intention is really to go through reference, if you have a good reason to use it, then that’s okay. And in fact, it’s rarely necessary. Almost always when someone thinks of using a reference, they are probably thinking about the performance or did not find a better way to do the same, so the use is wrong.
Almost whenever the intention is to pass by reference, it should do so with a type that is naturally passed with this semantics.
But for everything there is reason to use or not to use. Like everything, use if really know what you’re doing, if you understand all the implications. And don’t use it if you don’t know why you’re using it. Not everyone understands what happens to data when you use a reference. It is not so complicated, but it is easy to forget that you are manipulating a reference and this has consequences on all writing performed in that variable.
I understood the use of reference in argument that is different from parameter. But after Zuul’s reply I paid more attention to the example and saw something different.
You do have a problem.
It is one thing to say that you want to pass an argument by reference, according to the title of the question, it is quite another to ask in the parameter that the argument be received by reference implicitly. That is, who calls does not know that is passing by reference something that should be passed by value in normal conditions.
But what kind of language does that allow? PHP, of course. And the language maintainers have outdone themselves. I didn’t know but newer versions made the problem even worse.
Forcing the caller to use a reference is not so bad. Think about it. If the caller is explicitly by way of reference, it shows that it knows that this semantics is being used, what harm can it cause? At most, what I quoted, the programmer did not know right what this means.
Then a possibility of language improvement would be to force the call to use the reference in the argument when the parameter was annotated to be received by reference.
But what did she do? Forbade to do :D This... is... in-believable!
Then the new versions made the reference impractical for those who like organized code, expressive, code that indicates their intention and does not give room to misinterpretation. As the reference passage is now required to be implicit use it if you want to create confusion.
That would be the "right":
<?php
function something(&$string) {
$string .= 'B';
}
$variavel = 'A';
something(&$variavel); //<============ Note aqui
echo $variavel; //Irá mostrar "AB"
But this does not compile. What was certain was that this was mandatory. So the "signature" of the call matches the signature of the function definition. That is, both have the reference.
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
I’m not saying that careful programmers can’t program using implicit references without causing problems. But careful programmers code in C, in Assembly :) What I mean is that PHP should not be a language where you have to have this kind of concern. Actually this specifically, nor does C. In C you have to be explicit.
Of course I’m not talking about the types that are known to be by reference, so how is it known to every programmer is easier to understand.
In conclusion, there is a problem of readability, yes, but that does not mean that it should not be used under any circumstances. I would really avoid it and when I needed to use it, I would document it ostensibly.
Good Zuul, I think I understand the use "wrong" is not in
function foo(&$a){}
is in theecho foo(&a);
for example, i.e.function foo(&$a)
will not issue error, but echoecho foo(&$a)
go, correct me if I’m wrong. Grateful :) +1, can I release an issue? To make this clearer :)– Guilherme Nascimento