I've created Singleton class below but how do I know that only one instance has been created. How do I test it? Also, is there any difference between self::$instance = new FileProcessor()
and self::$instance = new static()
or which one should I use?
Thanks
CLASS
class FileProcessor
{
protected static $instance = null;
public static function get_instance()
{
if (self::$instance === null)
{
self::$instance = new FileProcessor();
//self::$instance = new static();
}
return self::$instance;
}
}
$obj_1 = FileProcessor::get_instance();
echo '<pre>'; print_r($obj_1);
$obj_2 = FileProcessor::get_instance();
echo '<pre>'; print_r($obj_2);
OUTPUT
FileProcessor Object
(
)
FileProcessor Object
(
)