-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 712209a
Showing
6 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/vendor/ | ||
index.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# tonton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "carloswph/tonton", | ||
"description": "Serie of tools based on traits that simplify the use of singletons, multitons and other class instances controllers.", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "WP Helpers", | ||
"email": "carlos@wp-helpers.com", | ||
"homepage": "https://wp-helpers.com" | ||
} | ||
], | ||
"minimum-stability": "dev", | ||
"require": {}, | ||
"autoload": { | ||
"psr-4": { | ||
"TonTon\\": "src/" | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace TonTon; | ||
|
||
trait Multiton { | ||
|
||
/** | ||
* @var reference to multiton array of instances | ||
*/ | ||
private static $instances = []; | ||
|
||
/** | ||
* Creates a new instance of a multiton class flagged with a key. | ||
* | ||
* @param $key the key which the instance should be stored/retrieved | ||
* | ||
* @return self | ||
*/ | ||
final public static function instance($key) | ||
{ | ||
if(!array_key_exists($key, self::$instances)) { | ||
|
||
self::$instances[$key] = new self; | ||
} | ||
|
||
return self::$instances[$key]; | ||
} | ||
|
||
/** | ||
* Prevents calling the class using the new keyword, if the __construct is protected. | ||
* | ||
* @return void | ||
*/ | ||
private function __construct() {} | ||
|
||
/** | ||
* Prevents cloning the multiton instances. | ||
* | ||
* @return void | ||
*/ | ||
final private function __clone() {} | ||
|
||
/** | ||
* Prevents unserializing the multiton instances. | ||
* | ||
* @return void | ||
*/ | ||
final private function __wakeup() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace TonTon; | ||
|
||
trait Singleton { | ||
|
||
/** | ||
* @var reference to singleton instance | ||
*/ | ||
private static $instance; | ||
|
||
/** | ||
* Creates a new instance of a singleton class (via late static binding), | ||
* accepting a variable-length argument list. | ||
* | ||
* @return self | ||
*/ | ||
final public static function instance() | ||
{ | ||
if(!self::$instance) { | ||
|
||
self::$instance = new self; | ||
} | ||
|
||
return self::$instance; | ||
} | ||
|
||
/** | ||
* Prevents calling the class using the new keyword, if the __construct is protected. | ||
* | ||
* @return void | ||
*/ | ||
private function __construct() {} | ||
|
||
/** | ||
* Prevents cloning the singleton instance. | ||
* | ||
* @return void | ||
*/ | ||
final private function __clone() {} | ||
|
||
/** | ||
* Prevents unserializing the singleton instance. | ||
* | ||
* @return void | ||
*/ | ||
final private function __wakeup() {} | ||
} |