I try set session var in __destruct() method. Method __destruct() is running, but session var not set. While, session in __contruct() or other methods (e.g. test()) working as expected.
public function test()
{
$_SESSION['MyVarTest'] = rand(200,300); ← working correctly
}
public function __destruct()
{
echo 'called';
$_SESSION['MyVar'] = rand(1,100); ← not working
}
Updated version. Now i try native PHP Session and Symfony component, and both not working in __destruct() method.
<?php
namespace Project\Modules\Cart\Storage;
use Illuminate\Support\Collection;
class Session
{
/**
* @var \Symfony\Component\HttpFoundation\Session\Session
*/
protected $session;
protected $cart;
public function __construct()
{
$this->cart = new Collection();
$this->session = app('session');
print_r($_SESSION);
}
public function test()
{
$this->session->set('json', rand(1,100)); ← working correctly
$_SESSION['json'] = rand(1,100); ← working correctly
return $this->cart->toJson();
}
public function __destruct()
{
echo 'called';
$_SESSION['MyVar'] = rand(1,100); ← not working
$this->session->set('cart', serialize($this->cart->toArray())); ← not working
}
}