无法在php中删除对象属性

时间:2016-01-05 22:33:32

标签: php oop magic-methods

我有这段代码:

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中添加&,但这没有帮助。

可以找到整个代码here

1 个答案:

答案 0 :(得分:6)

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

我做了1次更改:添加了魔法__unset方法。

另外,我原本认为构造函数需要公开,但是进一步查看它我错了(所以我的测试代码不会工作,除非构造函数是公共的......反正......)。

以下是更新后的代码:

<?
class Session {
    public $storage;
    public $token;
    public $username;
    public $browser;
    public $start;
    public $last_access;
    private function __construct($u, $t, $s = null, $b = null, $d = null) {
        $this->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()。