Explorar el Código

Added Singleton interface

Taddeus Kroes hace 13 años
padre
commit
522e75f09b
Se han modificado 3 ficheros con 22 adiciones y 1 borrados
  1. 17 0
      base.php
  2. 1 1
      session.php
  3. 4 0
      tests/SingletonTestCase.php

+ 17 - 0
base.php

@@ -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.
  * 

+ 1 - 1
session.php

@@ -10,7 +10,7 @@ namespace webbasics;
 
 require_once 'base.php';
 
-class Session {
+class Session implements Singleton {
 	private static $instance;
 	
 	static function getInstance() {

+ 4 - 0
tests/SingletonTestCase.php

@@ -9,6 +9,10 @@ abstract class SingletonTestCase extends PHPUnit_Framework_TestCase {
 		$this->rclass = new ReflectionClass($this->getClassName());
 	}
 	
+	function testSingletonInterface() {
+		$this->assertTrue($this->rclass->implementsInterface('webbasics\Singleton'));
+	}
+	
 	function testConstructorPrivateness() {
 		$rmethod = new ReflectionMethod($this->getClassName(), '__construct');
 		$this->assertTrue($rmethod->isPrivate());