Giter Club home page Giter Club logo

php-object-freezer's Introduction

Object_Freezer

This class provides the low-level functionality required to store ("freeze") PHP objects to and retrieve ("thaw") PHP objects from an object store.

Object_Freezer::freeze()

Freezes an object.

In the example below, we freeze an object of class A. As this object aggregates an object of class B, the object freezer has to freeze two objects in total.

<?php
require_once 'Object/Freezer.php';

class A
{
    protected $b;

    public function __construct()
    {
        $this->b = new B;
    }
}

class B
{
    protected $foo = 'bar';
}

$freezer = new Object_Freezer;
var_dump($freezer->freeze(new A));
?>

Below is the output of the code example above.

array(2) {
  ["root"]=>
  string(36) "32246c35-f47b-4fbc-a2ad-ed14e520865e"
  ["objects"]=>
  array(2) {
    ["32246c35-f47b-4fbc-a2ad-ed14e520865e"]=>
    array(3) {
      ["className"]=>
      string(1) "A"
      ["isDirty"]=>
      bool(true)
      ["state"]=>
      array(2) {
        ["b"]=>
        string(57) "__php_object_freezer_3cd682bf-8eba-4fec-90e2-ebe98aa07ab7"
        ["__php_object_freezer_hash"]=>
        string(40) "8b80da9c38c0c41c829cbbefbca9b18aa67ff607"
      }
    }
    ["3cd682bf-8eba-4fec-90e2-ebe98aa07ab7"]=>
    array(3) {
      ["className"]=>
      string(1) "B"
      ["isDirty"]=>
      bool(true)
      ["state"]=>
      array(2) {
        ["foo"]=>
        string(3) "bar"
        ["__php_object_freezer_hash"]=>
        string(40) "e04e935f09f2d526258d8a16613c5bce31e84e87"
      }
    }
  }
}

If the object has not been frozen before, the attribute __php_object_freezer_uuid will be added to it.

The reference to the object of class B that the object of class A had before it was frozen has been replaced with the UUID of the frozen object of class B (__php_object_freezer_3cd682bf-8eba-4fec-90e2-ebe98aa07ab7).

The result array's root element contains the UUID for the now frozen object of class A (32246c35-f47b-4fbc-a2ad-ed14e520865e).

Object_Freezer::thaw()

Thaws an object.

<?php
require_once 'Object/Freezer.php';

require_once 'A.php';
require_once 'B.php';

$freezer = new Object_Freezer;

var_dump(
  $freezer->thaw(
    array(
      'root'    => '32246c35-f47b-4fbc-a2ad-ed14e520865e',
      'objects' => array(
        '32246c35-f47b-4fbc-a2ad-ed14e520865e' => array(
          'className' => 'A',
          'isDirty'   => FALSE,
          'state'     => array(
            'b' => '__php_object_freezer_3cd682bf-8eba-4fec-90e2-ebe98aa07ab7',
          ),
        ),
        '3cd682bf-8eba-4fec-90e2-ebe98aa07ab7' => array(
          'className' => 'B',
          'isDirty'   => FALSE,
          'state'     => array(
            'foo' => 'bar',
          )
        )
      )
    )
  )
);
?>

Below is the output of the code example above.

object(A)#3 (2) {
  ["b":protected]=>
  object(B)#5 (2) {
    ["foo":protected]=>
    string(3) "bar"
    ["__php_object_freezer_uuid"]=>
    string(36) "3cd682bf-8eba-4fec-90e2-ebe98aa07ab7"
  }
  ["__php_object_freezer_uuid"]=>
  string(36) "32246c35-f47b-4fbc-a2ad-ed14e520865e"
}

Object_Freezer_Storage_CouchDB

This class provides the means to use CouchDB as an object storage.

<?php
require_once 'Object/Freezer/Storage/CouchDB.php';

class A
{
    protected $b;

    public function __construct()
    {
        $this->b = new B;
    }
}

class B
{
    protected $foo = 'bar';
}

$storage = new Object_Freezer_Storage_CouchDB(
  'database', NULL, NULL, TRUE, 'localhost', 5984
);

$id = $storage->store(new A);

var_dump($storage->fetch($id));
?>

Below is the output of the code example above.

object(A)#3 (2) {
  ["b":protected]=>
  object(B)#5 (2) {
    ["foo":protected]=>
    string(3) "bar"
    ["__php_object_freezer_uuid"]=>
    string(36) "3cd682bf-8eba-4fec-90e2-ebe98aa07ab7"
  }
  ["__php_object_freezer_uuid"]=>
  string(36) "32246c35-f47b-4fbc-a2ad-ed14e520865e"
}

php-object-freezer's People

Contributors

beberlei avatar janl avatar sebastianbergmann avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.