Last active
December 20, 2015 15:39
-
-
Save kentatogashi/6156082 to your computer and use it in GitHub Desktop.
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 | |
/* class SingletonSample {...} */ | |
?> | |
<?php | |
/** | |
* get instance | |
*/ | |
$instance1 = SingletonSample::getInstance(); | |
sleep(3); | |
$instance2 = SingletonSample::getInstance(); | |
echo '<hr>'; | |
/** | |
* compare two ids. | |
*/ | |
echo 'instance ID : ' . $instance1->getID() . '<br>'; | |
echo '$instance1->getID() === $instance2->getID() : ' . ($instance1->getID() === $instance2->getID() ? 'true' : 'false'); | |
echo '<hr>'; | |
/** | |
* compare two instances. | |
*/ | |
echo '$instance1 === $instance2 : ' . ($instance1 === $instance2 ? 'true' : 'false'); | |
echo '<hr>'; | |
/** | |
* confirm that you can not clone the instance. | |
* you get RuntimeExpection error. | |
*/ | |
$instance1_clone = clone $instance1; | |
/** | |
* also you can not use 'new instance', | |
* get fatal error. | |
**/ | |
$instance_new = new SingletonSample; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment