<?php

/* This is Singleton design pattern sample for PHP5.
*
* $Id: singleton.php,v 1.1 2004/02/12 15:33:19 tea Exp tea $
*/


class Singleton {
    private static
$instance = NULL;

    private function
__construct() {
        print
"called constructor\n";
    }

    public static function
instance() {
        if (
self::$instance == NULL) {
            
self::$instance = new Singleton;
        }
        return
self::$instance;
    }
}

$obj1 = Singleton::instance();  // Make instance on the first time only.
$obj2 = Singleton::instance();  // This doesn't call constructor.

// Error: You can not make instance, because of private constructor.
// $obj3 = new Singleton;

if ($obj1 === $obj2) {
    print
"$obj1 and $obj2 are same instance.\n";
}
else {
    print
"$obj1 and $obj1 are not same instance.\n";
}

?>