Will trigger an error if someone tries to clone the object (bypassing the essence of this class).
Code: Select all
<?php
// +----------------------------------------------------------------------------
// | Author: Tim Van Wassenhove <timvw@users.sourceforge.net>
// | Update: 2005-01-22 07:11
// |
// | A singleton class.
// +----------------------------------------------------------------------------
class Singleton
{
private static $singleton;
private function __construct()
{
echo "inside constructor method.";
}
public static function getInstance()
{
if (self::$singleton == null)
{
self::$singleton = new Singleton;
}
return self::$singleton;
}
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
}
$instance1 = Singleton::getInstance();
$instance2 = Singleton::getInstance();
$instance3 = clone($instance1);
?>feyd | fixed spelling error in function call example.