-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Always return the same instance of a const
Implement EnumManager to keep track of all instances
- Loading branch information
Showing
3 changed files
with
74 additions
and
4 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
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,43 @@ | ||
<?php | ||
/** | ||
* @link http://github.com/myclabs/php-enum | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file) | ||
*/ | ||
|
||
namespace MyCLabs\Enum; | ||
|
||
use ReflectionObject; | ||
|
||
/** | ||
* Enum instance manager | ||
* | ||
* @internal | ||
*/ | ||
abstract class EnumManager | ||
{ | ||
/** | ||
* Store existing Enum instances. | ||
* | ||
* @var array | ||
*/ | ||
private static $instances = array(); | ||
|
||
/** | ||
* Returns the Enum instance for the given prototype | ||
* | ||
* @return Enum | ||
*/ | ||
public static function get(Enum $enum) | ||
{ | ||
$reflection = new ReflectionObject($enum); | ||
$class = $reflection->getName(); | ||
$name = $enum->getKey(); | ||
|
||
if (isset(self::$instances[$class][$name])) { | ||
return self::$instances[$class][$name]; | ||
} | ||
|
||
self::$instances[$class][$name] = $enum; | ||
return $enum; | ||
} | ||
} |
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