|
1 |
| -# tonton |
| 1 | +# TonTon PHP |
| 2 | + |
| 3 | +Singleton patterns are amongst the most used structures in PHP. Together with that, some similar constructions, such as the Multiton, appear in almost every new PHP project. However, singletons could be much more. Initially, they don't really need to be inserted in any class, several times. Some people use abstracts, but does it make sense, extending a class which is just a support, by definition? |
| 4 | + |
| 5 | +Also, some patterns could be deriving from the Singleton, beyond the Multiton, so that is what we are proposing here: |
| 6 | + |
| 7 | +* First, use Singletons and similar as traits, to avoid duplicates and repetition while coding |
| 8 | +* Second, create new and different subpatterns that can elevate the initial purpose of a Singleton to something even more useful |
| 9 | + |
| 10 | +# Installation |
| 11 | + |
| 12 | +TonTon is available as PHP library, being installed through Composer. |
| 13 | + |
| 14 | +# Usage |
| 15 | + |
| 16 | +Well, for the first two traits, "using" them is pretty much what you need to do. As traits, once they are required with Composer, they can easily be added to any class through the `use` keyword inside the class. The usage is quite simple - let's consider a class named `EasyPeasy`: |
| 17 | + |
| 18 | +```php |
| 19 | + |
| 20 | +require __DIR__ . '/vendor/autoload.php'; |
| 21 | + |
| 22 | +class EasyPeasy { |
| 23 | + |
| 24 | + use \TonTon\Singleton; |
| 25 | + |
| 26 | + protected function construct() { |
| 27 | + |
| 28 | + echo 'Easy Peasy!'; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/* So, if we want to instantiate this class, then we need to use a |
| 33 | + static method. Any new attempts of instantiating the class will |
| 34 | + result in nothing (if we try to use the static method again) or |
| 35 | + a fatal error, if we try to create an instance through the "new" |
| 36 | + keyword.*/ |
| 37 | + |
| 38 | +$ep = EasyPeasy::instance(); // Valid instance |
| 39 | +$rt = EasyPeasy::instance(); // Nothing happens, as an instance already exists |
| 40 | + |
| 41 | +$er = new EasyPeasy(); // Fatal error |
| 42 | + |
| 43 | + |
| 44 | +``` |
| 45 | +But the Singleton here is just the tip of the iceberg. If you want, by any reason, to have multiple instances of your class, but all of them need to be flagged (let's say one for dealing with queries, and other for logging processes), you'd prefer to use a Multiton instead. The approach will be basically the same: |
| 46 | + |
| 47 | +```php |
| 48 | + |
| 49 | +require __DIR__ . '/vendor/autoload.php'; |
| 50 | + |
| 51 | +class EasyPeasy { |
| 52 | + |
| 53 | + use \TonTon\Multiton; |
| 54 | + |
| 55 | + protected function construct() { |
| 56 | + |
| 57 | + echo 'Easy Peasy!'; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/* So, if we want to instantiate this class, then we need to use a |
| 62 | + static method. Any new attempts of instantiating the class will |
| 63 | + work if using the static method instance(), with a $key as argument |
| 64 | + or a fatal error, if we try to create an instance through the "new" |
| 65 | + keyword.*/ |
| 66 | + |
| 67 | +$ep = EasyPeasy::instance('normal'); // Instance 1 |
| 68 | +$rt = EasyPeasy::instance('log'); // Instance 2 |
| 69 | + |
| 70 | +$er = new EasyPeasy(); // Fatal error |
| 71 | + |
| 72 | + |
| 73 | +``` |
| 74 | +But let's say, besides using many instances of that class, you want the number of instances to be limit, and you'd like to set this limit while creating your class. That can now be done using the Limiton: |
| 75 | + |
| 76 | + |
| 77 | +```php |
| 78 | + |
| 79 | +require __DIR__ . '/vendor/autoload.php'; |
| 80 | + |
| 81 | +class EasyPeasy { |
| 82 | + |
| 83 | + use \TonTon\Limiton; |
| 84 | + |
| 85 | + protected function construct() { |
| 86 | + |
| 87 | + $this->setLimit(1); // Limit set to 1, so it works like a Singleton, |
| 88 | + // but needs a $key for the new instance. Default |
| 89 | + // value is 2. |
| 90 | + echo 'Easy Peasy!'; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/* So, if we want to instantiate this class, then we need to use a |
| 95 | + static method. Any new attempts of instantiating the class will |
| 96 | + work if using the static method instance(), with a $key as argument |
| 97 | + or a fatal error, if we try to create an instance through the "new" |
| 98 | + keyword.*/ |
| 99 | + |
| 100 | +$ep = EasyPeasy::instance('normal'); // Instance 1 |
| 101 | +$rt = EasyPeasy::instance('log'); // Nothing happens, as we limited the max number |
| 102 | + // of instances in 1 for this class. |
| 103 | + |
| 104 | +$er = new EasyPeasy(); // Fatal error |
| 105 | + |
| 106 | + |
| 107 | +``` |
0 commit comments