<?php
Final class Singleton
{
/**
* @var This class of * Single example *
/private static $instance;
/** *
Returns a single example of this class * * *
@return Singleton the *singleton* instance.
*/public
static function getinstance ()
{
if (null = = static:: $instance) {
static:: $instance = new Static ();
return static:: $instance;
}
/**
* Declares the constructor as protected to prevent new instances from being created outside this class with the ' new ' operator
/Private Function __construct ()
{
}
/**
* The Clone method is declared private to prevent the cloning of a single case
*
* @return void
/Private Function __clone ()
{
}
/**
* Declares the deserialization method private to prevent deserialization of a single instance
*
* @return void/
Private Function __ Wakeup ()
{
}
}
$obj = Singleton::getinstance ();
Var_dump ($obj = = = Singleton::getinstance ()); BOOL (TRUE)
Turn from: http://laravel-china.github.io/php-the-right-way/pages/Design-Patterns.html