我有这个代码:
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,但这没有帮助.
整个代码可以在这里找到.
你真的应该在你的帖子中发布你的Session类,而不是链接到你的GitHub repo ...这就是为什么评论令人困惑.您正在使用会话类的魔术方法.
我改变了1:添加了magic __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().