note 71203 added to language.oop5.constants
| From: | gtatrealvertex dot com at osu1 dot php dot net | Date: | Thu, 16 Nov 2006 06:20:38 +0000 |
| Subject: | note 71203 added to language.oop5.constants | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119797@lists.php.net to get a copy of this message | ||
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
----
Server IP: 216.194.113.175
Probable Submitter: 71.227.160.115
----
Manual Page -- http://www.php.net/manual/en/language.oop5.constants.php
Edit -- https://master.php.net/note/edit/71203
Del: integrated -- https://master.php.net/note/delete/71203/integrated
Del: useless -- https://master.php.net/note/delete/71203/useless
Del: bad code -- https://master.php.net/note/delete/71203/bad+code
Del: spam -- https://master.php.net/note/delete/71203/spam
Del: non-english -- https://master.php.net/note/delete/71203/non-english
Del: in docs -- https://master.php.net/note/delete/71203/in+docs
Del: other reasons-- https://master.php.net/note/delete/71203
Reject -- https://master.php.net/note/reject/71203
Search -- https://master.php.net/manage/user-notes.php