. PHP Design Patterns . PHP Design Patterns Singleton

PHP Design Patterns Singleton

About the Singleton

In the singleton pattern a class can distribute one instance of itself to other classes.

BookSingleton.php


//copyright Lawrence Truett and FluffyCat.com 2005, all rights reserved
  
  class BookSingleton {

    private $author = 'Gamma, Helm, Johnson, and Vlissides';
    private $title  = 'Design Patterns';
    private static $book = NULL;
    private static $isLoanedOut = FALSE;

    private function __construct() {
    }

    static function borrowBook() {
      if (FALSE == self::$isLoanedOut) {
        if (NULL == self::$book) {
           self::$book = new BookSingleton();
        }
        self::$isLoanedOut = TRUE;
        return self::$book;
      } else {
        return NULL;
      }
    }

    function returnBook(BookSingleton $bookReturned) {
        self::$isLoanedOut = FALSE;
    }

    function getAuthor() {return $this->author;}

    function getTitle() {return $this->title;}

    function getAuthorAndTitle() {
      return $this->getTitle() . ' by ' . $this->getAuthor();
    }

  }


download source, use right-click and "Save Target As..." to save with a .php extension.

BookBorrower.php


//copyright Lawrence Truett and FluffyCat.com 2005, all rights reserved

  include_once('BookSingleton.php');

  Class BookBorrower {

    private $borrowedBook;
    private $haveBook = FALSE;

    function __construct() {
    }

    function getAuthorAndTitle() {
      if (TRUE == $this->haveBook) {
        return $this->borrowedBook->getAuthorAndTitle();
      } else {
        return "I don't have the book";
      }
    }

    function borrowBook() {
      $this->borrowedBook = BookSingleton::borrowBook();
      if ($this->borrowedBook == NULL) {
        $this->haveBook = FALSE;
      } else {
        $this->haveBook = TRUE;
      }
    }

    function returnBook() {
      $this->borrowedBook->returnBook($this->borrowedBook);
    }

  }

download source, use right-click and "Save Target As..." to save with a .php extension.

testSingleton.php


//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved

  include_once('BookSingleton.php');
  include_once('BookBorrower.php');

  writeln('BEGIN TESTING SINGLETON PATTERN');
  writeln('');

  $bookBorrower1 = new BookBorrower();
  $bookBorrower2 = new BookBorrower();

  $bookBorrower1->borrowBook();
  writeln('BookBorrower1 asked to borrow the book');
  writeln('BookBorrower1 Author and Title: ');
  writeln($bookBorrower1->getAuthorAndTitle());
  writeln('');

  $bookBorrower2->borrowBook();
  writeln('BookBorrower2 asked to borrow the book');
  writeln('BookBorrower2 Author and Title: ');
  writeln($bookBorrower2->getAuthorAndTitle());
  writeln('');

  $bookBorrower1->returnBook();
  writeln('BookBorrower1 returned the book');
  writeln('');

  $bookBorrower2->borrowBook();
  writeln('BookBorrower2 Author and Title: ');
  writeln($bookBorrower1->getAuthorAndTitle());
  writeln('');

  writeln('END TESTING SINGLETON PATTERN');

  function writeln($line_in) {
    echo $line_in.'<'.'BR'.'>';
  }


download source, use right-click and "Save Target As..." to save with a .php extension.

output of testSingleton.php

BEGIN TESTING SINGLETON PATTERN

BookBorrower1 asked to borrow the book BookBorrower1 Author and Title: Design Patterns by Gamma, Helm, Johnson, and Vlissides

BookBorrower2 asked to borrow the book BookBorrower2 Author and Title: I don't have the book

BookBorrower1 returned the book

BookBorrower2 Author and Title: Design Patterns by Gamma, Helm, Johnson, and Vlissides

END TESTING SINGLETON PATTERN

notes

As Tim pointed out, "make sure to throw an error when clone is called.. Because a singleton shouldn't be cloned :)" I didn't do that, but it in mind if cloning is a concern in your singleton implementation.

Special thanks to Olie for suggesting that I should "make the constructor private, and the borrowBook() method static".

References

Design Patterns

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

PHP 5

The Official PHP web site
Core PHP Programming, 3rd Edition by Leon Atkinson and Zeev Suraski
Sign In
to add the first comment for PHP Design Patterns Singleton.