Last active
August 29, 2015 14:02
-
-
Save jonataa/949be3b10ac0a4eabd5b to your computer and use it in GitHub Desktop.
Exemplo – Padrão de Projeto – Singleton
This file contains hidden or 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 | |
| class Singleton | |
| { | |
| protected static $instance; | |
| public $texto = 'Padrão'; | |
| private function __construct() | |
| { // Não faz nada | |
| } | |
| public static function getInstance() | |
| { | |
| if (null === self::$instance) | |
| self::$instance = new Singleton; | |
| return self::$instance; | |
| } | |
| } | |
| $obj = Singleton::getInstance(); | |
| echo $obj->texto . PHP_EOL; // Padrão | |
| $obj->texto = 'Olá Mundo!'; | |
| $obj2 = Singleton::getInstance(); | |
| echo $obj2->texto; // Olá Mundo! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment