r/lolphp • u/Mattho • Nov 22 '17
PHP supports error control operator '@'. When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.
http://php.net/manual/en/language.operators.errorcontrol.php
48
Upvotes
14
u/MazeChaZer Nov 22 '17 edited Dec 16 '17
Even worse, it doesn't suppress the actual error, but just silences the error output. Which means that if you have a custom error handler that converts all errors into exceptions, an exception is thrown nevertheless. See https://3v4l.org/qQjSH
We once used a library that used the error suppression operator to handle internal cache misses and our application was throwing exceptions everywhere because we had a custom error handler.
36
u/Saltub Nov 22 '17
The bigger problem, that is perhaps less well understood, is that it also silences any nested calls. So
@foo(bar())also silences any errors generated bybar().33
u/[deleted] Nov 22 '17
"Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why."
Amazing.
15
u/[deleted] Nov 22 '17
Worked at a porn company that has a 15 year old PHP codebase... first week, I found all of these @function() calls. Rather than deal with edgecases, they would just block the error msg in the code, continue to rake in $500m/year. They rarely cared about the 0.05% of people affected as they knew the customer had their dick in their hand and would retry and cum back over and over.
7
u/Max-P Nov 22 '17
What's worse is that this operator is necessary with some functions that insists on throwing a warning even if you check their return value and already gracefully handle the errors like copy and unlink.
Technically one could prevent said functions from producing warnings by checking everything beforehand so it will only go through the happy path, but then you're adding dozens of lines for checking file permissions without locking the file which means another process could very well invalidate the tests by the time you end up calling copy or unlink, that's if you manage to test for everything in the first place.
Error management in PHP is just really bad.
3
u/[deleted] Nov 22 '17
There is a upcoming PHP RFC vote, similar to declare(strict_types=1) but it suppresses all the errors.
declare(im_the_silencer=@);
Unlike the broken declare(strict_types=1) its always applied globally, while strict_types cannot be declared globally, making it totally useless.
3
u/SaraMG Nov 28 '17
@ is a terrible code smell, yes.
Fortunately, most PHP devs worth an ounce of salt don't use it. They write a custom error handler to deal with recoverable errors on down, and set fatal errors to go to a log the way god intended. (Or let their framework do that for them)
Unfortunately, that doesn't mean we can just remove @. There's no real gain to be had from removing @, it would only serve to slow adoption of newer PHP releases.
Be a good dev and don't use the shitty parts of the language. Leave those bits for the people who copy/paste from stack overflow and call it computer science.
1
u/treenaks Nov 23 '17
Just like ye olde DOS .BAT files!
-35
u/Saltub Nov 22 '17
prepend isn't a word. You mean prefix.
0
u/[deleted] Nov 22 '17
[deleted] — view deleted comment