PHP Design Patterns .

PHP Design Patterns Memento

About the Memento

In the Memento pattern a memento object will hold the state of another object.

In this example, the BookMark class is the "Memento", and holds the state of the BookReader class. The BookReader class would be the "Originator" in this example, as it had the original state. TestMemento.php holds the BookMark object, and so is the "Caretaker".

The memento should be set up so that the caretaker can create, set, and get memento values for an originator. However, the caretaker itself can not access any of the values stored in the memento.

In my example I do this by having memento only allow calls to it's get and set functions in which it is passed a BookReader object. The BookMark can then get or set the titles or pages for a bookreader object it is passed. The downside of my implementation is that I have BookReader's get and set functions as public.


  class BookReader {
    private $title;    
    private $page;
    function __construct($title_in, $page_in) {
    public function getPage() {
      return $this->page;
    public function setPage($page_in) {
      $this->page = $page_in;

public function getTitle() { return $this->title; } public function setTitle($title_in) { $this->title = $title_in; } }
To download source right-click here and "Save As...".


  //this is the memento

include_once('BookReader.php'); class BookMark { private $title; private $page; function __construct(BookReader $bookReader) { $this->setPage($bookReader); $this->setTitle($bookReader); } public function getPage(BookReader $bookReader) { $bookReader->setPage($this->page); } public function setPage(BookReader $bookReader) { $this->page = $bookReader->getPage(); } public function getTitle(BookReader $bookReader) { $bookReader->setTitle($this->title); } public function setTitle(BookReader $bookReader) { $this->title = $bookReader->getTitle(); } }
To download source right-click here and "Save As...".


//copyright Lawrence Truett and 2005, all rights reserved

include_once('BookReader.php'); include_once('BookMark.php');

echo tagins("html"); echo tagins("head"); echo tagins("/head"); echo tagins("body");

echo "BEGIN TESTING MEMENTO PATTERN"; echo tagins("br").tagins("br"); $bookReader = new BookReader("Core PHP Programming, Third Edition","103"); $bookMark = new BookMark($bookReader); echo "(at beginning) bookReader title: ". $bookReader->getTitle().tagins("br"); echo "(at beginning) bookReader page: ". $bookReader->getPage().tagins("br"); $bookReader->setPage("104"); $bookMark->setPage($bookReader); echo "(one page later) bookReader page: ". $bookReader->getPage().tagins("br"); $bookReader->setPage('2005'); //oops! a typo echo "(after typo) bookReader page: ". $bookReader->getPage().tagins("br"); $bookMark->getPage($bookReader); echo "(back to one page later) bookReader page: ". $bookReader->getPage().tagins("br");

echo tagins("br"); echo "END TESTING MEMENTO PATTERN"; echo tagins("br"); echo tagins("/body"); echo tagins("/html"); //doing this so code can be displayed without breaks function tagins($stuffing) { return "<".$stuffing.">"; }
To download source right-click here and "Save As...".

output of testMemento.php


(at beginning) bookReader title: Core PHP Programming, Third Edition (at beginning) bookReader page: 103 (one page later) bookReader page: 104 (after typo) bookReader page: 2005 (back to one page later) bookReader page: 104



Design Patterns
Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides

The Official PHP web site
Core PHP Programming, 3rd Edition by Leon Atkinson and Zeev Suraski
Sign in to be the first to comment on PHP Design Patterns Memento.