From 58b40059d63445dd7564e813774dfa5d7ce7d490 Mon Sep 17 00:00:00 2001 From: supergrecko Date: Sat, 27 Jul 2019 18:31:41 +0200 Subject: [PATCH] Added singleton article --- .../docs/php/design-patterns/singleton.md | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 src/content/docs/php/design-patterns/singleton.md diff --git a/src/content/docs/php/design-patterns/singleton.md b/src/content/docs/php/design-patterns/singleton.md new file mode 100644 index 0000000..a25be15 --- /dev/null +++ b/src/content/docs/php/design-patterns/singleton.md @@ -0,0 +1,116 @@ +--- +authors: + - "@supergrecko" +created_at: "2019/07/27" +--- + +# Singletons + +A singleton is a class which is only instantiated once during runtime. This is done by keeping a static property containing its instance on the singleton class. + +There are multiple benefits to using a singleton class + +- You're always going to pull the same instance of the class +- It's only instantiated once +- Use `$this` in a static-like context +- + +By using a singleton instead of a static class we expose a cleaner class to use and we can use regular instance properties instead of static properties. + +# Creating a Singleton in PHP + +Creating a class which can be used as a singleton is very simple. + +```php +word . PHP_EOL; + } + + public function setWord(string $word): void { + $this->word = $word; + } +``` + +We are now ready to test our Singleton. + +We'll start of by proving that only one instance is created during runtime. We'll grab the singleton instance twice using `Singleton::getInstance();` and comparing their object ids using `spl_object_hash`. Let's try it! + +```php +$first = Singleton::getInstance(); +$second = Singleton::getInstance(); + +// Compare the hash ids for each of the variables, if they are equal then they contain the same instance. +var_dump(spl_object_hash($first) === spl_object_hash($second)); // bool(true) +``` + +We can now prove its static-like functionality by using our `getWord` and `setWord` methods. + +We will do this by comparing the returned value from `getWord()` on `$first` and `$second`. + +```php +var_dump($first->getWord() === $second->getWord()); // bool(true) + +// now let's try chaning the value on $first +$first->setWord("Banana"); + +// the test will still pass, because it's a singleton. +var_dump($first->getWord() === $second->getWord()); // bool(true) +``` + +# Further Research + +Here's a couple links which will help you understand Singletons better. + +## Singleton Resources: + +- https://en.wikipedia.org/wiki/Singleton_pattern +- https://phpenthusiast.com/blog/the-singleton-design-pattern-in-php +- https://phptherightway.com/pages/Design-Patterns.html#singleton + +## Static Keyword Resources: + +- https://www.php.net/manual/en/language.oop5.static.php +- https://www.php.net/manual/en/language.oop5.late-static-bindings.php +- https://en.wikipedia.org/wiki/Static_(keyword) + +## The Null-Coalescing Operator Resources: + +- https://en.wikipedia.org/wiki/Null_coalescing_operator +- https://www.php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op