Skip to content

Instantly share code, notes, and snippets.

@kanonji
Created October 16, 2014 10:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanonji/9a298488998179ac1f62 to your computer and use it in GitHub Desktop.
Save kanonji/9a298488998179ac1f62 to your computer and use it in GitHub Desktop.
Const map trait
<?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