dev-master
9999999-devCommon programing patterns
GPL-3.0
The Requires
- php >=5.6
patterns
1.0.0
1.0.0.0Common programing patterns
GPL-3.0
The Requires
- php >=5.6
patterns
Common programing patterns
Common programing patterns for evo, (*1)
Multiton (trait and interface), (*2)
If you don't know what a Singlton is I suggest reading about it, (*3)
https://en.wikipedia.org/wiki/Singleton_pattern, (*4)
Singleton useage, (*5)
``` use evo\pattern\singleton\SingletonTrait; use evo\pattern\singleton\SingletonInterface;, (*6)
Class Foo implements SingletonInterface{ use Singleton; protected function init(){ //stuff to do on __construct } }, (*7)
Then you can call your class like this (each additional call will return the same instance of the class)
$Foo = Foo:getInstance();, (*8)
Multiton usage
use evo\pattern\singleton\MultitonTrait; use evo\pattern\singleton\MultitonInterface;, (*9)
Class Foo implements MultitonInterface{ use Multiton; protected function init(){ //stuff to do on __construct } } ```, (*10)
Then you can call your class like this, (*11)
$Foo = Foo:getInstance();
The diffrence between them is the Multiton acts more like a container for Singlton instances. So you can call the getInstance
method multiple times with diffrent aliases and get multiple copies of the class, each a singleton in it's own right. Becareful using Static proprites on multitons as they can be a bit tricky., (*12)
``` //this will produce two singleton instances of the class $Foo1 = Foo:getInstance('instance1'); $Foo2 = Foo:getInstance('instance2');, (*13)
Install via composer
{ "require" : { "evo/patterns" : "~1.0" } } ``` I plan to add a few more to this, as needed., (*14)
Common programing patterns
GPL-3.0
patterns
Common programing patterns
GPL-3.0
patterns