note 71203 deleted from language.oop5.constants by felipe
| From: | felipe@php.net | Date: | Sat, 15 Dec 2007 02:09:25 +0000 |
| Subject: | note 71203 deleted from language.oop5.constants by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133526@lists.php.net to get a copy of this message | ||
Note Submitter: gt at realvertex.com
----
If you would like to generate a constant names dynamically, then one could use the functions
defined() and constant():
class CONFIG {
const MYCONST1 = 'Hello ';
const MYCONST2 = 'World';
public static function get($_name) {
if(defined("self::$_name"))
return constant("self::$_name");
throw new Exception('Constant ' . $_name . ' is not defined');
}
}
$vars = array(1,2);
try {
foreach($vars as $const_suffix) {
echo CONFIG::get('MYCONST' . $const_suffix);
}
} catch (Exception $e) {
$msg = $e->getMessage();
echo $msg;
}
Output will print:
Hello World