Singleton Classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class AbstractSingleton implements SingletonInterface | |
{ | |
final public static function getInstance() | |
{ | |
static $instance; | |
return $instance = $instance ?: new \static(); | |
} | |
final private function __construct() {} | |
final private function __clone() {} | |
final private function __sleep() {} | |
final private function __wakeup() {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class AbstractSingleton implements SingletonInterface | |
{ | |
use SingletonTrait; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface SingletonInterface { | |
final public static function getInstance(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
trait SingletonTrait { | |
final public static function getInstance(){ | |
static $instance; | |
return $instance = $instance ?: new \static(); | |
} | |
final private function __construct() {} | |
final private function __clone() {} | |
final private function __sleep() {} | |
final private function __wakeup() {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// usages | |
class Singleton implements SingletonInterface{ | |
use SingletonTrait; | |
} | |
$instance = Singleton::getInstance(); | |
class Singleton extends AbstractSingleton { | |
} | |
$instance = Singleton::getInstance(); | |
class Singleton extends AbstractSingletonTrait { | |
} | |
$instance = Singleton::getInstance(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment