Taking advantage of the @Lias hook, if the non-existence of constants is something serious in your application, a more consistent alternative to error message return in functions is the use of exceptions, thus:
if (empty($dump))
throw new Exception("Atenção: Não foram localizadas constantes com o prefixo '".$prefix."'");
However, if it is just something informative, log the message and return an empty list.
Furthermore, consider also the principle of single responsibility and, depending on the purpose of this message, place this if
in the "client" code that calls the method. See an example:
define('CON_WEBSITE_01', 'John');
(...)
$constants = get_constantsByPrefix('CON_WEBSITE_');
print_r( empty($constants) ? "Nada encontrado" : $constants );
Performance of the function get_constantsByPrefix()
, depending on the use case, we may consider the following factors:
Constants can be defined in the "middle" of the main system execution or they are usually defined on startup, for example, in addition of classes at the beginning of the script?
Depending on the case, it would be worth storing the return map for later use, rather than always iterating over all constants.
Let’s scribble an example using a class:
class Constants {
private $constant_map = null;
public static function listByPrefix($prefix) {
if ($this->constant_map == null) {
$this->constant_map = array();
foreach (get_defined_constants() as $key=>$value) {
if (substr($key,0,strlen($prefix))==$prefix) {
$this->constant_map[$key] = $value;
}
}
}
if (empty($this->constant_map)) {
throw new Exception("Atenção: Não foram localizadas constantes com o prefixo '".$prefix."'");
} else {
return $this->constant_map;
}
}
}
Example of use:
define('CON_WEBSITE_01', 'John');
Constants::listByPrefix('CON_WEBSITE_');
You have control over the definition of constants?
In this case, you could encapsulate their creation with a function that already stores them on the map.
Another sketch:
class Constants {
private $constant_map = array();
public static function define($key, $value) {
define($key);
$this->constant_map[$key] = $value;
}
public static function listByPrefix($prefix) {
if (empty($this->constant_map)) {
throw new Exception("Atenção: Não foram localizadas constantes com o prefixo '".$prefix."'");
} else {
return $this->constant_map;
}
}
}
Example of use:
Constants::define('CON_WEBSITE_01', 'John');
Constants::listByPrefix('CON_WEBSITE_');
Are you experiencing performance problems? What is your motivation to optimize? At first glance it seems to me a concise function and without any serious efficiency problems or points for improvement.
– mgibsonbr
@mgibsonbr For now I have no particular problem, but yes, I worry about the performance because the function is used in our administrative area that after implementation will serve hundreds of people, at the same time it will contain an exorbitant number of constants.
– Zuul
However, the main reason is the practice of submitting to the community new functions that I use or create to collect feedback and/or improvements (Usually someone thinks of something I haven’t thought of or predicted) :)
– Zuul
I get it. I asked because I don’t have much to suggest, no, unless the
get_defined_constants
is slow (and then, in the absence of an alternative medium, only one cache would be left - preferably ordered, so that a binary search can be used to avoid scrolling through the entire list).– mgibsonbr
@bigown Nothing yet regarding tags, better wait for Marc Gravell’s ok. For now cedilha only works in goal.
– bfavaretto