@@ -4,19 +4,19 @@ |
||
4 | 4 | |
5 | 5 | class Example |
6 | 6 | { |
7 | - use \ByJG\DesignPattern\Singleton; |
|
7 | + use \ByJG\DesignPattern\Singleton; |
|
8 | 8 | |
9 | - protected $_uniqId; |
|
9 | + protected $_uniqId; |
|
10 | 10 | |
11 | - protected function __construct() |
|
12 | - { |
|
13 | - $this->_uniqId = rand(0, 1000); |
|
14 | - } |
|
11 | + protected function __construct() |
|
12 | + { |
|
13 | + $this->_uniqId = rand(0, 1000); |
|
14 | + } |
|
15 | 15 | |
16 | - public function getId() |
|
17 | - { |
|
18 | - return $this->_uniqId; |
|
19 | - } |
|
16 | + public function getId() |
|
17 | + { |
|
18 | + return $this->_uniqId; |
|
19 | + } |
|
20 | 20 | |
21 | 21 | } |
22 | 22 |
@@ -24,10 +24,10 @@ |
||
24 | 24 | // This works; |
25 | 25 | // Must return the same ID; |
26 | 26 | $example1 = Example::getInstance(); |
27 | -echo $example1->getId() . "\n"; |
|
27 | +echo $example1->getId()."\n"; |
|
28 | 28 | |
29 | 29 | $example2 = Example::getInstance(); |
30 | -echo $example2->getId() . "\n"; |
|
30 | +echo $example2->getId()."\n"; |
|
31 | 31 | |
32 | 32 | |
33 | 33 | // That cannot work! |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -require_once __DIR__ . "/../vendor/autoload.php"; |
|
3 | +require_once __DIR__."/../vendor/autoload.php"; |
|
4 | 4 | require_once "Sample1.php"; |
5 | 5 | require_once "Sample2.php"; |
6 | 6 |
@@ -6,51 +6,51 @@ |
||
6 | 6 | |
7 | 7 | class SingletonTest extends \PHPUnit\Framework\TestCase |
8 | 8 | { |
9 | - public function testSingleton() |
|
10 | - { |
|
11 | - $sample1 = Sample1::getInstance(); |
|
12 | - $this->assertEquals(10, $sample1->property); |
|
13 | - $sample1->property = 20; |
|
14 | - $this->assertEquals(20, $sample1->property); |
|
15 | - |
|
16 | - $sample1Other = Sample1::getInstance(); |
|
17 | - $this->assertEquals(20, $sample1Other->property); |
|
18 | - $sample1->property = 40; |
|
19 | - $this->assertEquals(40, $sample1Other->property); |
|
20 | - $this->assertEquals(40, $sample1->property); |
|
21 | - |
|
22 | - // |
|
23 | - |
|
24 | - $sample2 = Sample2::getInstance(); |
|
25 | - $sample2->property2 = 50; |
|
26 | - $this->assertEquals(50, $sample2->property2); |
|
27 | - |
|
28 | - $sample2Other = Sample2::getInstance(); |
|
29 | - $this->assertEquals(50, $sample2Other->property2); |
|
30 | - $sample2->property2 = 90; |
|
31 | - $this->assertEquals(90, $sample2Other->property2); |
|
32 | - $this->assertEquals(90, $sample2->property2); |
|
33 | - |
|
34 | - // |
|
35 | - |
|
36 | - $this->assertEquals(40, $sample1->property); |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * @expectedException \ByJG\DesignPattern\SingletonException |
|
41 | - */ |
|
42 | - public function testClone() |
|
43 | - { |
|
44 | - $sample1 = Sample1::getInstance(); |
|
45 | - $sample2 = clone $sample1; |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * @expectedException \ByJG\DesignPattern\SingletonException |
|
50 | - */ |
|
51 | - public function testSerialize() |
|
52 | - { |
|
53 | - $sample1 = Sample1::getInstance(); |
|
54 | - $serialize = serialize($sample1); |
|
55 | - } |
|
9 | + public function testSingleton() |
|
10 | + { |
|
11 | + $sample1 = Sample1::getInstance(); |
|
12 | + $this->assertEquals(10, $sample1->property); |
|
13 | + $sample1->property = 20; |
|
14 | + $this->assertEquals(20, $sample1->property); |
|
15 | + |
|
16 | + $sample1Other = Sample1::getInstance(); |
|
17 | + $this->assertEquals(20, $sample1Other->property); |
|
18 | + $sample1->property = 40; |
|
19 | + $this->assertEquals(40, $sample1Other->property); |
|
20 | + $this->assertEquals(40, $sample1->property); |
|
21 | + |
|
22 | + // |
|
23 | + |
|
24 | + $sample2 = Sample2::getInstance(); |
|
25 | + $sample2->property2 = 50; |
|
26 | + $this->assertEquals(50, $sample2->property2); |
|
27 | + |
|
28 | + $sample2Other = Sample2::getInstance(); |
|
29 | + $this->assertEquals(50, $sample2Other->property2); |
|
30 | + $sample2->property2 = 90; |
|
31 | + $this->assertEquals(90, $sample2Other->property2); |
|
32 | + $this->assertEquals(90, $sample2->property2); |
|
33 | + |
|
34 | + // |
|
35 | + |
|
36 | + $this->assertEquals(40, $sample1->property); |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * @expectedException \ByJG\DesignPattern\SingletonException |
|
41 | + */ |
|
42 | + public function testClone() |
|
43 | + { |
|
44 | + $sample1 = Sample1::getInstance(); |
|
45 | + $sample2 = clone $sample1; |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * @expectedException \ByJG\DesignPattern\SingletonException |
|
50 | + */ |
|
51 | + public function testSerialize() |
|
52 | + { |
|
53 | + $sample1 = Sample1::getInstance(); |
|
54 | + $serialize = serialize($sample1); |
|
55 | + } |
|
56 | 56 | } |
@@ -4,12 +4,12 @@ |
||
4 | 4 | |
5 | 5 | class Sample1 |
6 | 6 | { |
7 | - use \ByJG\DesignPattern\Singleton; |
|
7 | + use \ByJG\DesignPattern\Singleton; |
|
8 | 8 | |
9 | - public $property; |
|
9 | + public $property; |
|
10 | 10 | |
11 | - private function __construct() |
|
12 | - { |
|
13 | - $this->property = 10; |
|
14 | - } |
|
11 | + private function __construct() |
|
12 | + { |
|
13 | + $this->property = 10; |
|
14 | + } |
|
15 | 15 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -require_once __DIR__ . "/../vendor/autoload.php"; |
|
3 | +require_once __DIR__."/../vendor/autoload.php"; |
|
4 | 4 | |
5 | 5 | class Sample1 |
6 | 6 | { |
@@ -4,7 +4,7 @@ |
||
4 | 4 | |
5 | 5 | class Sample2 |
6 | 6 | { |
7 | - use \ByJG\DesignPattern\Singleton; |
|
7 | + use \ByJG\DesignPattern\Singleton; |
|
8 | 8 | |
9 | - public $property2; |
|
9 | + public $property2; |
|
10 | 10 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -require_once __DIR__ . "/../vendor/autoload.php"; |
|
3 | +require_once __DIR__."/../vendor/autoload.php"; |
|
4 | 4 | |
5 | 5 | class Sample2 |
6 | 6 | { |
@@ -4,47 +4,47 @@ |
||
4 | 4 | |
5 | 5 | trait Singleton |
6 | 6 | { |
7 | - protected function __construct() |
|
8 | - { |
|
9 | - } |
|
10 | - |
|
11 | - /** |
|
12 | - * @throws SingletonException |
|
13 | - */ |
|
14 | - final public function __clone() |
|
15 | - { |
|
16 | - throw new SingletonException('You can not clone a singleton.'); |
|
17 | - } |
|
18 | - |
|
19 | - /** |
|
20 | - * @throws SingletonException |
|
21 | - */ |
|
22 | - final public function __sleep() |
|
23 | - { |
|
24 | - throw new SingletonException('You can not serialize a singleton.'); |
|
25 | - } |
|
26 | - |
|
27 | - /** |
|
28 | - * @throws SingletonException |
|
29 | - */ |
|
30 | - final public function __wakeup() |
|
31 | - { |
|
32 | - throw new SingletonException('You can not deserialize a singleton.'); |
|
33 | - } |
|
34 | - |
|
35 | - /** |
|
36 | - * @return static |
|
37 | - */ |
|
38 | - public static function getInstance() |
|
39 | - { |
|
40 | - static $instances; |
|
41 | - |
|
42 | - $calledClass = get_called_class(); |
|
43 | - |
|
44 | - if (!isset($instances[$calledClass])) { |
|
45 | - $instances[$calledClass] = new $calledClass(); |
|
46 | - } |
|
47 | - return $instances[$calledClass]; |
|
48 | - } |
|
7 | + protected function __construct() |
|
8 | + { |
|
9 | + } |
|
10 | + |
|
11 | + /** |
|
12 | + * @throws SingletonException |
|
13 | + */ |
|
14 | + final public function __clone() |
|
15 | + { |
|
16 | + throw new SingletonException('You can not clone a singleton.'); |
|
17 | + } |
|
18 | + |
|
19 | + /** |
|
20 | + * @throws SingletonException |
|
21 | + */ |
|
22 | + final public function __sleep() |
|
23 | + { |
|
24 | + throw new SingletonException('You can not serialize a singleton.'); |
|
25 | + } |
|
26 | + |
|
27 | + /** |
|
28 | + * @throws SingletonException |
|
29 | + */ |
|
30 | + final public function __wakeup() |
|
31 | + { |
|
32 | + throw new SingletonException('You can not deserialize a singleton.'); |
|
33 | + } |
|
34 | + |
|
35 | + /** |
|
36 | + * @return static |
|
37 | + */ |
|
38 | + public static function getInstance() |
|
39 | + { |
|
40 | + static $instances; |
|
41 | + |
|
42 | + $calledClass = get_called_class(); |
|
43 | + |
|
44 | + if (!isset($instances[$calledClass])) { |
|
45 | + $instances[$calledClass] = new $calledClass(); |
|
46 | + } |
|
47 | + return $instances[$calledClass]; |
|
48 | + } |
|
49 | 49 | |
50 | 50 | } |