Skip to content

Add caching to toArray method #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 11, 2013
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion 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 instantiated reflection objects in a static cache.
* @var array
*/
protected static $reflectionCache = array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be private, I see no use to making it accessible to subclasses

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Completely agree. Let me get this sorted.


/**
* Creates a new value of some type
Expand Down Expand Up @@ -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();
}

Expand Down