Ancestors

Toot

Written by dseguy on 2024-12-11 at 20:33

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

=> View attached media

=> More informations about this toot | More toots from dseguy@phpc.social

Descendants

Written by bob.php :veritrek_gold: on 2024-12-11 at 21:04

@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

Written by dseguy on 2024-12-16 at 21:12

@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

Written by bob.php :veritrek_gold: on 2024-12-16 at 21:25

@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

Written by Alerta! Alerta! on 2024-12-11 at 21:31

@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

Written by Ben Ramsey on 2024-12-11 at 21:43

@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

Written by Ben Ramsey on 2024-12-11 at 21:45

@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

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/113636097432256414
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
314.506245 milliseconds
Gemini-to-HTML Time
1.899067 milliseconds

This content has been proxied by September (ba2dc).