A small persistence class for PHP, integrable with any kind of persistence storage (file, SQL, …)
/* * CREATE TABLE table_name ( * class VARCHAR(20) PRIMARY KEY, * val BLOB NOT NULL * ); */ class Persistence { private $table = 'val'; public function __construct(){ $this->db = new Database($DSN); } public function save($classname, &;$object){ $obj = serialize($object); $this->;db->;query("REPLACE INTO ".$this->table." SET class = ?, val = ?;", array($classname, $obj)); return $this->db->num_rows(); } public function get($classname){ $this->db->query("SELECT val FROM ".$this->table." WHERE class = ?", $classname); $res = $this->db->row_obj(); if(is_null($res)) return false; return @unserialize($res->val); } } |
Here an example with Pear HTTP_Client class.
$pers = new Persistence(); if($eppclient = $pers->get(‘HTTPClient’)) $client =& $eppclient; else{ $client = new HTTP_Client(); $client->_cookieManager->serializeSessionCookies(true); $this->kp->save(‘HTTPClient’, $client); } |