From 4ed0a017c32be8228814862bcbcaa783d123a55f Mon Sep 17 00:00:00 2001 From: Gareth Evans Date: Mon, 11 Nov 2013 16:45:59 +0000 Subject: [PATCH 1/3] Add caching to `toArray` method Reflection can be slow on occasions and this optimization should be beneficial if `toArray` is being called more than once per request. --- src/MyCLabs/Enum/Enum.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/MyCLabs/Enum/Enum.php b/src/MyCLabs/Enum/Enum.php index a22a6a3..a918bfe 100755 --- a/src/MyCLabs/Enum/Enum.php +++ b/src/MyCLabs/Enum/Enum.php @@ -20,6 +20,12 @@ abstract class Enum * @var mixed */ protected $value; + + /** + * Store instantiated reflection objects in a static cache. + * @var array + */ + protected static $reflectionCache = array(); /** * Creates a new value of some type @@ -57,7 +63,10 @@ public function __toString() */ public static function toArray() { - $reflection = new \ReflectionClass(get_called_class()); + $calledClass = get_called_class(); + if(!array_key_exists($calledClass, self::$reflectionCache)) { + self::$reflectionCache[$calledClass] = new \ReflectionClass($calledClass); + } return $reflection->getConstants(); } From 5d0b38e221970260f4d4559ac9ac646110e80f66 Mon Sep 17 00:00:00 2001 From: Gareth Evans Date: Mon, 11 Nov 2013 16:58:19 +0000 Subject: [PATCH 2/3] Minor changes to caching - Cache constants rather than reflection objects - Make static cache private --- src/MyCLabs/Enum/Enum.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/MyCLabs/Enum/Enum.php b/src/MyCLabs/Enum/Enum.php index a918bfe..158da73 100755 --- a/src/MyCLabs/Enum/Enum.php +++ b/src/MyCLabs/Enum/Enum.php @@ -22,10 +22,10 @@ abstract class Enum protected $value; /** - * Store instantiated reflection objects in a static cache. + * Store existing constants in a static cache per object. * @var array */ - protected static $reflectionCache = array(); + private static $constantsCache = array(); /** * Creates a new value of some type @@ -64,10 +64,11 @@ public function __toString() public static function toArray() { $calledClass = get_called_class(); - if(!array_key_exists($calledClass, self::$reflectionCache)) { - self::$reflectionCache[$calledClass] = new \ReflectionClass($calledClass); + if(!array_key_exists($calledClass, self::$constantsCache)) { + $reflectin = new \ReflectionClass($calledClass); + self::$constantsCache[$calledClass] = $reflection->getConstants(); } - return $reflection->getConstants(); + return self::$constantsCache[$calledClass]; } /** From 067f53e045f125c10eeea98fc64b9fd9ab4f21ca Mon Sep 17 00:00:00 2001 From: Gareth Evans Date: Mon, 11 Nov 2013 16:59:45 +0000 Subject: [PATCH 3/3] Typo --- src/MyCLabs/Enum/Enum.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MyCLabs/Enum/Enum.php b/src/MyCLabs/Enum/Enum.php index 158da73..a850588 100755 --- a/src/MyCLabs/Enum/Enum.php +++ b/src/MyCLabs/Enum/Enum.php @@ -65,7 +65,7 @@ public static function toArray() { $calledClass = get_called_class(); if(!array_key_exists($calledClass, self::$constantsCache)) { - $reflectin = new \ReflectionClass($calledClass); + $reflection = new \ReflectionClass($calledClass); self::$constantsCache[$calledClass] = $reflection->getConstants(); } return self::$constantsCache[$calledClass];