I would say it depends on your application's design. If the class is some general, loosly coupled class or module and suddenly it uses such global vars, that would be bad practice in my book. But if the class is clearly geared towards a specific task for which those specific global vars are needed anyway (eg your login example), I don't see any clear objections.
views:
357answers:
4To answer my own question, I'd say yes, it's fine to access super globals, provided you aren't modifying them to be accessed across multiple classes. There's no magic going on in the background - you're just reading the state, and super globals are how PHP provides that to you.
However, you should never, ever, ever modify a global within a class and access it somewhere else. That's when you make unit testing impossible.
One approach is to wrap all superglobals into a class of its own. I'm pretty sure Zend Framework has a class of its own for e.g. manipulating Cookies.
I wouldn't say there is a straight yes or no answer to this one. What the idea (with all superglobals $_GET
$_POST
$_SESSION
) is that you are asking for data that sits in your entire application, and not local to the scope you are asking for.
What can happen with these superglobals is that what if they change somewhere for whatever reason just before or (god forbid) during the execution of your function. That can turn out to be a very annoying bug to reproduce.
So I would say it is bad form.