Passing a literal by reference is a Fatal error,
while returning the same literal by reference is a notice.
[#]PHP : Why?
[#]phptip #phptrick
https://php-tips.readthedocs.io/en/latest/tips/by_reference_errors.html
=> More informations about this toot | More toots from dseguy@phpc.social
@dseguy do we know why or are we just guessing? remember back when conferences started adding talks about references and every codebase went to hell from using them wrongly 🤣
=> More informations about this toot | More toots from bobmagicii@phpc.social
@bobmagicii I don't know why.
My best guess is legacy reasons.
Any take?
=> More informations about this toot | More toots from dseguy@phpc.social
@dseguy the notice when returning the const array when you claimed you would return a ref is probably the engine angry that you cant do what you said you would do. given how php works with copy on write and all that its probably safe enough to let you be bad.
the fatal when supplying the data though is likely some hard stability or optimisation reason in the engine.
the difference between letting an adult wander into traffic but stopping a kid? thats my theory.
=> More informations about this toot | More toots from bobmagicii@phpc.social
@dseguy Passing a literal value by reference doesn't make sense and is probably a case of "you are doing it wrong". Think about passing a literal array to sort doesn't make sense....
OTOH returning a reference to some static value still allows me to change that value - even though it might not male much sense.
Alao usually the person calling a function is much more likely to do something stupid than the person writing that function.... 🙈
It might not be the real reason, but it works for me 😁
=> More informations about this toot | More toots from heiglandreas@phpc.social
@heiglandreas @dseguy Passing a literal array to sort() is a common mistake because most people probably expect it to return the sorted array. After 25 years of using sort(), I’ve even found myself doing this and forgetting that it doesn’t return the sorted array.
=> More informations about this toot | More toots from ramsey@phpc.social
@heiglandreas @dseguy A “literal” is a bad example in this case because, if I had a literal, I would probably handwrite it already sorted, but you get the point. 😁
=> More informations about this toot | More toots from ramsey@phpc.social This content has been proxied by September (ba2dc).Proxy Information
text/gemini