Created
October 16, 2014 10:40
-
-
Save kanonji/9a298488998179ac1f62 to your computer and use it in GitHub Desktop.
Const map trait
This file contains hidden or 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
<?php | |
trait ConstMapTrait{ | |
private static $const_map = []; | |
public static function const_map($prefix = 0){ | |
if(! isset(self::$const_map[$prefix])){ | |
if(0 === $prefix){ | |
self::$const_map[$prefix] = (new \ReflectionClass(get_called_class()))->getConstants(); | |
} else { | |
self::const_map(); | |
$list = self::$const_map[0]; | |
// $filterd = array_filter($list, function($key){ return 0 === stripos($key, $prefix); }, ARRAY_FILTER_USE_KEY); // PHP 5.6 | |
$filterd = []; | |
foreach($list as $key => $value){ | |
if( 0 === stripos($key, $prefix) ) $filterd[$key] = $value; | |
} | |
self::$const_map[$prefix] = $filterd; | |
} | |
} | |
return array_flip(self::$const_map[$prefix]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment