加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP析构函数未被调用

发布时间:2020-12-13 17:15:45 所属栏目:PHP教程 来源:网络整理
导读:我正在使用 PHP 5.4.12 我有两节课 ?phpclass MySessionHandler implements SessionHandlerInterface {//..}$handler = new MySessionHandler();session_set_save_handler($handler,true);? 和 ?phpclass MySession {//..function __destruct() { session_wr
我正在使用 PHP 5.4.12

我有两节课

<?php

class MySessionHandler implements SessionHandlerInterface {
//..
}

$handler = new MySessionHandler();
session_set_save_handler($handler,true);

?>

<?php
class MySession {
//..
function __destruct() {
     session_write_close();
     echo 'called';
    }
}

使用此代码,MySession的析构函数永远不会被调用.

<?php
require_once 'MySessionHandler.php';
include_once 'MySession.php';
$test = new MySession();

但是使用这段代码,“MySession”析构函数被称为ok

<?php
require_once 'MySessionHandler.php';
include_once 'MySession.php';

class Test {
    function __construct() {
        $test = new MySession();
    }
}
$obj = new Test();

我测试了session_set_save_handler($handler,true / false).
我需要调用MySession的析构函数,因为我遇到了session_write_close();我必须明确地调用它,否则会话永远不会被写入服务器.

关于这个的任何解决方法?我已经将代码简化为它们的根,但是__destruct方法在应该的时候不会被调用.

解决方法

不要依赖__destruct … From the manual:

When using objects as session save handlers,it is important to register the shutdown function with PHP to avoid unexpected side-effects from the way PHP internally destroys objects on shutdown and may prevent the write and close from being called. Typically you should register ‘session_write_close’ using the register_shutdown_function() function.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读