Cool PHP Objects Sleep on the Couch
The Object_Freezer library for PHP provides the low-level functionality to store ("freeze") and fetch ("thaw") any PHP userland object to and from arbitrary object stores.
Today I added an object storage implementation that uses Apache CouchDB as its backend.
Apache CouchDB, the distributed, fault-tolerant and schema-free document-oriented database that provides scalability as a consequence of its design, is a natural fit for such an object store.
The following example shows how the Object_Freezer's new backend for CouchDB can be used to persist PHP objects:
<?php
require 'Object/Freezer/Storage/CouchDB.php';
class Foo
{
public $a;
protected $b;
private $c;
public function __construct($a, $b, $c)
{
$this->a = $a;
$this->b = $b;
$this->c = $c;
}
}
$object = new Foo(1, 2, 3);
var_dump($object);
// Store the object in the database "test" of the
// CouchDB server that is running on localhost:5984.
$storage = new Object_Freezer_Storage_CouchDB('test');
$storage->store($object);
// Freezing the object added the "magic" attributes
// __php_object_freezer_uuid and __php_object_freezer_hash.
var_dump($object);
// Fetch the object (by ID) from the database.
var_dump($storage->fetch($object->__php_object_freezer_uuid));
?>
Below is the output produced by the script above:
object(Foo)#1 (3) { ["a"]=> int(1) ["b":protected]=> int(2) ["c":"Foo":private]=> int(3) } object(Foo)#1 (5) { ["a"]=> int(1) ["b":protected]=> int(2) ["c":"Foo":private]=> int(3) ["__php_object_freezer_uuid"]=> string(36) "8d17b95e-1410-4e54-a70c-064c09c79210" ["__php_object_freezer_hash"]=> string(40) "40cd89ec4ea3b5a1c1419c83b4ea643da5341ab8" } object(Foo)#10 (5) { ["a"]=> int(1) ["b":protected]=> int(2) ["c":"Foo":private]=> int(3) ["__php_object_freezer_uuid"]=> string(36) "8d17b95e-1410-4e54-a70c-064c09c79210" ["__php_object_freezer_hash"]=> string(40) "40cd89ec4ea3b5a1c1419c83b4ea643da5341ab8" }
Here is an overview of the current feature set:
- PHP objects, with the exception of objects of "special" classes such as
ClosureorPDO, can be frozen and thawed. - Graphs of objects, including those that contain circular references, are supported.
- Frozen objects can be stored in and fetched from an Apache CouchDB database.
0 Comments to "Cool PHP Objects Sleep on the Couch"
1 Trackback to "Cool PHP Objects Sleep on the Couch"
Add Comment