How to access superglobals in correct way?
Asked Answered
H

2

12

I'm discovering secrets of PHP. I've found one thing that I don't have answer to. I would like to access variables from super-global $_SESSION in class. Everything works but PHPMD engine to check mess in code is showing me issue.

I'm getting value from $_SESSION super-global this way

$value = $_SESSION["value"];

And I'm editting values of $_SESSION super-global this way

$_SESSION['value'] = "newValue";

PHPMD is showing me issue:

accesses the super-global variable $_SESSION.

So I'm finding another way how to edit and get values of super-global $_SESSION correctly.

I've tried to use filter_input, problem is that when I use INPUT_POST as type(argument 1), PHP shows me warning:

INPUT_SESSION is not yet implemented

Thanks for future answers :)

EDIT (Quotes from phpmd documentation)

Superglobals Since: PHPMD 0.2. Accessing a super-global variable directly is considered a bad practice. These variables should be encapsulated in objects that are provided by a framework, for instance.

Hallowed answered 17/7, 2016 at 17:23 Comment(4)
Why are you mashing up three topics here? The language feature, filter_input limitations and in particular PHPMD are unreleated.Compel
Look, I've described everything in question to let everyone know what do I want. It's hard to describe it without these 3 topics. I'm sorry that I mashed them in one question. Practically, I just want to know if there is any other way to access and edit $_SESSION super-global variableHallowed
The first one is the correct and only way. PHPMD "showing an issue" is just that, an advisory (and not a very clever one).Compel
I was thinking that, but you know, I was not sure, thats reason why I've created this question. Thanks for your timeHallowed
L
4

As the hint says, accessing the superglobals violates the encapsulation principle

A really basic approach would be:

class SessionObject
{
    public $vars;

    public function __construct() {
        $this->vars = &$_SESSION; //this will still trigger a phpmd warning
    }
}

$session = new SessionObject();
$session->vars['value'] = "newValue";

You can also have a look to the Symfony HttpFoundation Component for a full-fledged implementation

Lovellalovelock answered 4/8, 2016 at 17:24 Comment(1)
So the final solution is to encapsulate as done in your example, AND suppress the warning?Titular
P
1

It's only a "bad pratice", you can still access to superglobals directly, if you are a fan on "best pratices", create a small class like that:

class Session{

    public static function put($key, $value){
        $_SESSION[$key] = $value;
    }

    public static function get($key){
        return (isset($_SESSION[$key]) ? $_SESSION[$key] : null);
    }

    public static function forget($key){
        unset($_SESSION[$key]);
    }
}

And use in that way:

Session::put('foo', 'bar');
$bar = Session::get('foo');
Session::forget('foo');
Peeling answered 17/7, 2016 at 19:15 Comment(1)
your code will still trigger the warning, but now in your class.Versicle

© 2022 - 2024 — McMap. All rights reserved.