Skip to content

Instantly share code, notes, and snippets.

@kentatogashi
Last active December 20, 2015 15:39
Show Gist options
  • Save kentatogashi/6156082 to your computer and use it in GitHub Desktop.
Save kentatogashi/6156082 to your computer and use it in GitHub Desktop.
<?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