How to get access to superglobal?

I discover the secrets of PHP. I found one for which I have no answer. I would like to access variables from the super-global $ _SESSION in the class. Everything works, but the PHPMD mechanism for checking for clutter in the code shows me the problem.

I get the value from $ _SESSION super-global this way

$value = $_SESSION["value"];

And in this way I edit the values ​​of $ _SESSION super-global

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

PHPMD shows me a question:

refers to the superglobal variable $ _SESSION.

So, I find another way to edit and get super-global $ _SESSION values ​​correctly.

I tried using filter_input, the problem is that when I use INPUT_POST as a type (argument 1), PHP shows me a warning:

INPUT_SESSION not yet implemented

Thanks for the future answers :)

EDIT ( phpmd)

: PHPMD 0.2. . , , .

+4
2

, encapsulation

:

class SessionObject
{
    public $vars;

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

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

Symfony HttpFoundation Component

+2

" ", , " ", :

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]);
    }
}

:

Session::put('foo', 'bar');
$bar = Session::get('foo');
Session::forget('foo');
0

Source: https://habr.com/ru/post/1648160/


All Articles