__destruct() 메서드에서 set session var를 시도합니다. 메서드 __destruct()가 실행 중이지만 세션 var가 설정되지 않았습니다. __contruct() 또는 다른 메소드 (예 : test())의 세션이 예상대로 작동하는 동안.__destruct() 메서드에서 세션 변수를 설정할 수없는 이유는 무엇입니까?
public function test()
{
$_SESSION['MyVarTest'] = rand(200,300); ← working correctly
}
public function __destruct()
{
echo 'called';
$_SESSION['MyVar'] = rand(1,100); ← not working
}
업데이트 버전 이제는 네이티브 PHP 세션과 Symfony 컴포넌트를 시도하고 __destruct() 메소드에서 작동하지 않습니다.
<?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
}
}
그럼이 모든 것을 테스트하는 방법을 보여주세요. –
'__destruct()'가 확실히 호출되는지 어떻게 알 수 있습니까? –
예, 예제는 아직 완료되지 않았습니다. 내가 생각할 수있는 한 가지 질문 : 스크립트를 어떻게 중단합니까? 최고는 완전한 예일 것이고, 우리는 달릴 수 있습니다. –