note 92311 added to language.oop5.magic
| From: | charlie at dasserver dot com | Date: | Sat, 18 Jul 2009 22:16:55 +0000 |
| Subject: | note 92311 added to language.oop5.magic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158461@lists.php.net to get a copy of this message | ||
In response to: dhuseby domain getback tld com
As of (at least) php 5.2.6, when overloading the magic __sleep method no special tricks are needed
anymore for private and protected class variables to survive serialization.
Simple illustration:
class dbms {
protected $dbHost = NULL;
protected $dbUser = NULL;
protected $dbPassword = NULL;
protected $dbName = NULL;
protected $dbCharset = NULL;
public function __construct($host, $user, $password, $db, $charset="utf8") {
$this->dbHost = $host;
$this->dbUser = $user;
$this->dbPassword = $password;
$this->dbName = $db;
$this->dbCharset = $charset;
$this->connect();
}
public function __wakeup() {
$this->connect($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbName,
$this->dbCharset);
}
public function __sleep() {
$this->disconnect();
return array('dbHost', 'dbUser', 'dbPassword', 'dbName',
'dbCharset');
}
public function connect() {
// Code to connect to database
}
public function disconnect() {
// Code to disconnect from database
}
public function query($sql) {
// Code to query the database
}
public function secure($value) {
// Code to secure against sql injection
}
}
class account {
private $ary_attribs = array('name' => NULL, 'email' => NULL);
private $dbms = NULL;
public function __construct($account_name, $dbms) {
if (! $dbms instanceof dbms)
throw new Exception("Passed object is not of type DBMS.");
$this->dbms = $dbms;
$this->ary_attribs['account_name'] = $account_name;
$sqlquery = sprintf("UPDATE accounts SET last_login = %u WHERE name = '%s'",
time(), $this->dbms->secure($account_name));
$this->dbms->query($sqlquery);
}
public function __wakeup() {
$this->dbms->__wakeup();
}
public function __sleep() {
return array('ary_attribs', 'dbms');
}
public function __get($attrib) {
return $this->ary_attribs[$attrib];
}
public function __set($attrib, $value) {
$this->ary_attribs[$attrib] = $value;
return true;
}
}
$dbms = new dbms('localhost', 'alice', '123456', 'test');
$account = new account("bob", $dbms);
$account->email = "bob@internet.com";
$serialized = serialize($account);
$newObj = unserialize($serialized);
print_r($newObj); // Bob's email address should not be NULL
exit();
----
Server IP: 87.124.35.190
Probable Submitter: 80.14.62.56
----
Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php
Edit -- https://master.php.net/note/edit/92311
Del: integrated -- https://master.php.net/note/delete/92311/integrated
Del: useless -- https://master.php.net/note/delete/92311/useless
Del: bad code -- https://master.php.net/note/delete/92311/bad+code
Del: spam -- https://master.php.net/note/delete/92311/spam
Del: non-english -- https://master.php.net/note/delete/92311/non-english
Del: in docs -- https://master.php.net/note/delete/92311/in+docs
Del: other reasons-- https://master.php.net/note/delete/92311
Reject -- https://master.php.net/note/reject/92311
Search -- https://master.php.net/manage/user-notes.php