CHAPTER 12 ■ ENTERPRISE PATTERNS
static function setDSN( $dsn ) {
return self::instance()->set('dsn', $dsn);
}
}
This class uses serialization to save and restore individual properties. The get() function checks for
the existence of the relevant value file. If the file exists and has been modified since the last read, the
method unserializes and returns its contents. Because it’s not particularly efficient to open a file for each
variable you are managing, you might want to take a different approach here—placing all properties into
a single save file. The set() method changes the property referenced by $key both locally and in the save
file. It updates the $mtimes property. This is the array of modification times that is used to test save files.
Later, if get() is called, the file can be tested against the corresponding entry in $mtimes to see if it has
been modified since this object’s last write.
If the shm (System V shared memory) extension is enabled in your PHP install, you might use its
functions to implement an application registry. Here’s a simplified example:
namespace woo\base;
// ...
class MemApplicationRegistry extends Registry {
private static $instance;
private $values=array();
private $id;
const DSN=1;
private function __construct() {
$this->id = @shm_attach(55, 10000, 0600);
if (! $this->id ) {
throw new Exception("could not access shared memory");
}
}
static function instance() {
if (! isset(self::$instance) ) { self::$instance = new self(); }
return self::$instance;
}
protected function get( $key ) {
return shm_get_var( $this->id, $key );
}
protected function set( $key, $val ) {
return shm_put_var( $this->id, $key, $val );
}
static function getDSN() {
return self::instance()->get(self::DSN);
}
static function setDSN( $dsn ) {
return self::instance()->set(self::DSN, $dsn);