无法删除 php 中的对象 属性
Can't delete object property in php
我有这个代码:
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。
你真的应该在你的 post 中 post 编辑你的 Session class 而不是链接到你的 GitHub 仓库...这就是评论令人困惑的原因。您在会话 class.
中使用了魔术方法
我做了 1 处更改:添加魔法 __unset 方法。
此外,我曾认为构造函数需要 public 但在进一步查看时我错了(所以我的测试代码将无法工作,除非构造函数是 public.. . 无论如何...).
下面是更新后的代码 class:
<?
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);
}
查看有关您需要添加到 class:
的魔术 __unset 方法的文档
http://php.net/manual/en/language.oop5.overloading.php#object.unset
__unset() is invoked when unset() is used on inaccessible properties.
我有这个代码:
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。
你真的应该在你的 post 中 post 编辑你的 Session class 而不是链接到你的 GitHub 仓库...这就是评论令人困惑的原因。您在会话 class.
中使用了魔术方法我做了 1 处更改:添加魔法 __unset 方法。
此外,我曾认为构造函数需要 public 但在进一步查看时我错了(所以我的测试代码将无法工作,除非构造函数是 public.. . 无论如何...).
下面是更新后的代码 class:
<?
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);
}
查看有关您需要添加到 class:
的魔术 __unset 方法的文档http://php.net/manual/en/language.oop5.overloading.php#object.unset
__unset() is invoked when unset() is used on inaccessible properties.