1

Inside a function I want to return a super global variable. It shows undefined. but when i use '@' before this super global variable then it is done. i want to know the actual use of '@'.

below is my code

public function getSession(){
       return $_SESSION['login'];  
   }
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
ashik
  • 119
  • 1
  • 11

4 Answers4

9

There are some good use cases for using @ in PHP.

One great example is if you're writing object oriented code and you want a clean object api that throws exceptions when something goes wrong. You're designing your class, and your object performs, say, a file_get_contents call in there. In order to maintain a nice, self-contained object api, you'll want to throw an exception when something goes wrong.

@file_get_contents(...)

Prefixing the @ there allows you suppress the warning and ensure that the user of this object gets an exception. Instead, check for a false then throw your exception.

Why do you have to do this? Because php is a daft mix of functions that have no similarities or standards when compared with each other.


And with your specific example, which is nothing to do with @, you'd do an isset($_SESSION['login']).

Jimbo
  • 25,790
  • 15
  • 86
  • 131
9

You are looking at this the wrong way.

You should never access PHP superglobals from within the class to begin with. Instead you should use them as parameters, when creating the instance at the bootstrap stage of your code:

$request = new Request(
    $_POST, 
    $_GET, new Session($_SESSION)
);

Kinda like this.

Then this $request instance is passes to the class, which is handling your user input. This has two major benefits:

  • ability to control and alter the perceived user input at runtime with affecting global scope (in cases when you are working in a legacy codebase, where parts of it are still based on include-oriented programming)
  • ability to test your code independently from the webserver
zerkms
  • 249,484
  • 69
  • 436
  • 539
tereško
  • 58,060
  • 25
  • 98
  • 150
  • 4
    Basically I think Teresko is pointing out that in OOP in this case, you need an abstraction around your Request (so it's testable). And because you would typically compose this object in your composition root, the rest of your application would then use this Request object and no superglobals. – Jimbo Jul 19 '16 at 08:38
2

This boils down to:

"When is it okay to use @?"

And the answer is: virtually never.

The @ operator suppresses errors, that's the only thing it does. There's basically no sane situation in which you'd ever want to suppress errors. Because error reports help you see, well, errors, mistakes, typos, wrong assumptions in your code. In this case your code is making the assumption that $_SESSION['login'] always exists. That assumption is apparently wrong, and PHP tells you about it with a notice. Either you have a bug in your code that causes this assumption to fail, or you need to change the assumption by using isset($_SESSION['login']).

The only legitimate case for using @ is if you're working with 3rd party code where you expect errors and know what they mean and know they are harmless and which cannot be suppressed any other way.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • 2
    The only legitimate use-case for `@` being working with third party code is factually incorrect. *Wait, didn't this happen already?* :D – Jimbo Jul 19 '16 at 14:35
  • 3
    @Jimbo If you count the PHP standard library as 3rd party count, it sounds about right... – NikiC Jul 19 '16 at 14:44
0

First of all there is no need to return a super gloab just because its super global somethign like this

public function Foo(){
    $_SESSION['login'] .= $_SESSIN['login'] . " blah blah";
}

its fine.

The @ is using for turning off notices. And thats why whe you use assume its working because you had just turn off the notice :). The $_SESSION is still undefined . Take a look at http://php.net/manual/en/language.operators.errorcontrol.php

dios231
  • 714
  • 1
  • 9
  • 21