当前位置:  开发笔记 > 编程语言 > 正文

无法在php中删除对象属性

如何解决《无法在php中删除对象属性》经验,为你挑选了1个好方法。

我有这个代码:

class Service {
    public function get_session($token) {
        foreach ($this->config->sessions as $session) {
            if ($token == $session->token) {
                $session->last_access = date('r');
                return $session;
            }
        }
        return null;
    }
    public function mysql_connect($token, $host, $username, $password, $db) {
        if (!$this->valid_token($token)) {
            throw new Exception("Access Denied: Invalid Token");
        }
        // will throw exception if invalid
        $this->mysql_create_connection($host, $username, $password, $db);
        $session = $this->get_session($token);
        $id = uniqid('res_');
        if (!isset($session->mysql)) {
            $session->mysql = new stdClass();
        }
        $mysql = &$session->mysql;
        $mysql->$id = array(
            'host' => $host,
            'user' => $username,
            'pass' => $password,
            'name' => $db
        );
        return $id;
    }
    public function mysql_close($token, $res_id) {
        if (!$this->valid_token($token)) {
            throw new Exception("Access Denied: Invalid Token");
        }
        $session = $this->get_session($token);
        if (!(isset($session->mysql->$res_id))) {
            throw new Exception("Invalid resource id");
        }
        unset($session->mysql->$res_id);
        if (empty((array)$session->mysql)) {
            unset($session->mysql); // this don't work, don't know why
            throw new Exception('isset($session->mysql) == ' .
                                (isset($session->mysql) ? 'true' : 'false'));
        }
    }
}

我调用unset($session->mysql);它是否为空但对象未被删除,异常抛出true,如何删除$session->mysql对象?我试图添加&get_session,但这没有帮助.

整个代码可以在这里找到.



1> Clay..:

你真的应该在你的帖子中发布你的Session类,而不是链接到你的GitHub repo ...这就是为什么评论令人困惑.您正在使用会话类的魔术方法.

我改变了1:添加了magic __unset方法.

此外,我曾经认为构造函数需要公开,但是进一步查看它我错了(所以我的测试代码将无法工作,除非构造函数是公共的......无论如何......).

以下是更新类的代码:

storage = $s ? $s : new stdClass();
        $this->username = $u;
        $this->token = $t;
        $this->browser = $b ? $b : $_SERVER['HTTP_USER_AGENT'];
        $this->start = $d ? $d : date('r');
    }
    function &__get($name) {
        return $this->storage->$name;
    }
    function __set($name, $value) {
        $this->storage->$name = $value;
    }
    function __isset($name) {
        return isset($this->storage->$name);
    }
    function __unset($name) {
        echo "Unsetting $name";
        unset($this->storage->$name);
    }
    static function create_sessions($sessions) {
        $result = array();
        foreach ($sessions as $session) {
            $result[] = new Session($session->username,
                                    $session->token,
                                    $session->storage,
                                    $session->browser,
                                    $session->start);
        }
        return $result;
    }
    static function cast($stdClass) {
        $storage = $stdClass->storage ? $stdClass->storage : new stdClass();
        return new Session($stdClass->username,
                           $stdClass->token,
                           $storage,
                           $stdClass->browser,
                           $stdClass->start);
    }
    static function new_session($username) {
        return new Session($username, token());
    }
}

还有一些测试代码:

$session = new Session('joe', '1234');
$session->mysql = 1234;
var_dump($session->mysql);
unset($session->mysql);
var_dump($session->mysql);

这是添加方法的代码:

function __unset($name) {
    echo "Unsetting $name";
    unset($this->storage->$name);
}

查看有关您需要添加到班级的magic __unset方法的文档:

http://php.net/manual/en/language.oop5.overloading.php#object.unset

当unset()用于不可访问的属性时,将调用__unset().

推荐阅读
手机用户2502851955
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有