Commit 522e75f0 authored by Taddeus Kroes's avatar Taddeus Kroes

Added Singleton interface

parent 6e91a09d
...@@ -103,6 +103,23 @@ class FileNotFoundError extends \Exception { ...@@ -103,6 +103,23 @@ class FileNotFoundError extends \Exception {
} }
} }
/**
* The Singleton interface mshould be implemented by classes that allow only
* one instance.
*
* The instance must be saved statically after the constructor has been
* called. When getInstance() is called another time, this instance is
* returned.
*/
interface Singleton {
/**
* Create a new singleton instance, and save it in the static $instance variable.
*
* @return object An existing instance from the $instance variable, or a new instance.
*/
public static function getInstance();
}
/** /**
* Format a string using parameters in an associative array. * Format a string using parameters in an associative array.
* *
......
...@@ -10,7 +10,7 @@ namespace webbasics; ...@@ -10,7 +10,7 @@ namespace webbasics;
require_once 'base.php'; require_once 'base.php';
class Session { class Session implements Singleton {
private static $instance; private static $instance;
static function getInstance() { static function getInstance() {
......
...@@ -9,6 +9,10 @@ abstract class SingletonTestCase extends PHPUnit_Framework_TestCase { ...@@ -9,6 +9,10 @@ abstract class SingletonTestCase extends PHPUnit_Framework_TestCase {
$this->rclass = new ReflectionClass($this->getClassName()); $this->rclass = new ReflectionClass($this->getClassName());
} }
function testSingletonInterface() {
$this->assertTrue($this->rclass->implementsInterface('webbasics\Singleton'));
}
function testConstructorPrivateness() { function testConstructorPrivateness() {
$rmethod = new ReflectionMethod($this->getClassName(), '__construct'); $rmethod = new ReflectionMethod($this->getClassName(), '__construct');
$this->assertTrue($rmethod->isPrivate()); $this->assertTrue($rmethod->isPrivate());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment