Skip to content

Commit

Permalink
Merge pull request #2 from garoevans/patch-1
Browse files Browse the repository at this point in the history
Add caching to `toArray` method
  • Loading branch information
mnapoli committed Nov 11, 2013
2 parents 4a27d4e + 067f53e commit b52c2f2
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/MyCLabs/Enum/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ abstract class Enum
* @var mixed
*/
protected $value;

/**
* Store existing constants in a static cache per object.
* @var array
*/
private static $constantsCache = array();

/**
* Creates a new value of some type
Expand Down Expand Up @@ -57,8 +63,12 @@ public function __toString()
*/
public static function toArray()
{
$reflection = new \ReflectionClass(get_called_class());
return $reflection->getConstants();
$calledClass = get_called_class();
if(!array_key_exists($calledClass, self::$constantsCache)) {
$reflection = new \ReflectionClass($calledClass);
self::$constantsCache[$calledClass] = $reflection->getConstants();
}
return self::$constantsCache[$calledClass];
}

/**
Expand Down

0 comments on commit b52c2f2

Please sign in to comment.