oop - Can't delete object property in php -


i have code:

class service {     public function get_session($token) {         foreach ($this->config->sessions $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");         }         // 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); // don't work, don't know why             throw new exception('isset($session->mysql) == ' .                                 (isset($session->mysql) ? 'true' : 'false'));         }     } } 

i call unset($session->mysql); if it's empty object not removed, exception throw true, how can delete $session->mysql object? i've tried add & in get_session didn't help.

whole code can found here.

you should have posted session class in post instead of linking github repo... that's why comments confusing. using magic methods on session class.

1 change made: adding magic __unset method.

also, had thought constructor needed public on further looking @ wrong (so test code not work unless constructor public... anyway...).

here code below updated 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 $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());     } } 

and test code:

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

this code of added method:

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

check out documentation magic __unset method need add class:

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

__unset() invoked when unset() used on inaccessible properties.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -