Skip to content

Commit 2212f72

Browse files
committed
Avoid to serialize/deserialize Singleton classes Issue #2
1 parent faa8b2b commit 2212f72

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Singleton.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ final public function __clone()
1616
throw new SingletonException('You can not clone a singleton.');
1717
}
1818

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+
1935
/**
2036
* @return static
2137
*/

tests/SingletonTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,12 @@ public function testClone()
4545
$sample2 = clone $sample1;
4646
}
4747

48+
/**
49+
* @expectedException \ByJG\DesignPattern\SingletonException
50+
*/
51+
public function testSerialize()
52+
{
53+
$sample1 = Sample1::getInstance();
54+
$serialize = serialize($sample1);
55+
}
4856
}

0 commit comments

Comments
 (0)