Is this bad practice use of the error suppression operator?

丶灬走出姿态 提交于 2019-12-04 16:57:26

Save yourself some coding and make an "Input" class with several static functions, like this:

class Input {

      public static function get($key, $default = null)
      {
            return (array_key_exists($key, $_GET)) ? $_GET[$key] : $default;
      }

      // same thing for $_POST...

}

Then you could call your sanitize function like so...

sanitize(Input::get('page', 'Unspecified'));

Using the @ operator is, indeed, generally considered as bad practice.


In your case, it could be avoided, by splitting things in several steps :

  • testing the variable is set -- with isset()
  • working on it -- or not :
    • if set, sanitizing it
    • else, using a default value.

As the @ operator can be avoided, here... well, I would avoid it.


Notes :

  • masking errors is generally not such a good idea (in this case, it shouldn't hurt much... but, still)
  • and the @ operator has a cost, speaking of performances1.
  • one-liners is not a goal one should necessarily have ;-)


1. But some will say it doesn't matter that much -- and they are probably right

You could use the terany operator to test for existence, avoiding using the error suprression operator:

$Page = (!empty($_POST['test'])) ? $_POST['test'] : 'default';

Generally using the suppression operator is viewed as a bad practice, so using the terany operator like this would avoid suppressing errors as well as give you the desired effect.

The @ operator do not avoid the error, it makes it quite. But if you check for error you will have one. This is why it's a bad practice. But also because hiding errors generally brings troubles.

A good way is this:

$Page = (isset($_GET['page'])) ? $_GET['page'] : 'default';
$Page = sanitise($Page, "Unspecified");

But since you have a sanitise() function, you could upgrade it and make it this check for you.

function sanitise($value, $default, $fromRequest=false)  {
 if ($fromRequest) $value = (isset($_REQUEST[$value])) ? $_REQUEST$value] : $default;
  ..
}

$_REQUEST is the global variable that represents $_GET + $_POST + $_COOKIE, but you may cutomize my version.

In general, the other answers are correct. There are issues using @ to just pretend that errors don't exist.

That said, in this case, I'd use your approach. It's legible, concise and — in this small scenario — just does the job. I'm hard-pressed to think of potential bugs here.

To avoid the cargo cult programming syntax with isset I'm using object wrappers around the input arrays. It specifically does the checking behind the scenes, so I can avoid both the silly isset and @.

For your example I would write $_GET->int->default("time", time())
or $_GET->sanitize["page"] and if all rules are predefined just $_GET["whatever"] with automatic filtering.

Otherwise I'd still be using @$_GET, because I do not believe in appearance coding.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!