【发布时间】:2012-04-01 14:00:43
【问题描述】:
我是 CodeIgniter 的新手。我将它用于我的个人项目。
我想遵循的一件事是坚持最佳编码实践。其中之一是 DRY 规则(不要重复自己)。我尝试在适当的模型类中定义一些常量,并在代码中的其他任何地方使用它们,所以当涉及到一点改变时,只能在一个地方完成。
我有这个类Hero_attributes,它有几个常量,例如:
class Hero_attributes
{
const ID = 'id';
const NAME = 'name';
const LEVEL = 'level';
const MAG_LEVEL = 'maglevel';
const VOCATION = 'vocation';
const GENDER = 'sex';
const SPEED = 'speed'
(...)
}
我还制作了一个自动加载的自定义配置文件tenkai_config.php。在配置数组中,我想使用 Hero_attributes 类中的常量作为键,即:
$config['vocations']['default'] = array(
'hero' => array(
Hero_attributes::LAST_LOGIN => 0,
Hero_attributes::LEVEL => 1,
Hero_attributes::MAG_LEVEL => 80,
Hero_attributes::RESIDENCE => 1,
Hero_attributes::POSITION_X => 73,
Hero_attributes::POSITION_Y => 181,
Hero_attributes::POSITION_Z => 6,
(...)
)
);
但是,我收到了Fatal error: Class 'Hero_attributes' not found in C:\wamp_new\www\yourwodb\application\config\tenkai_config.php on line 29 消息。
如何在配置文件中使用常量?
【问题讨论】:
-
它不会工作,因为 config.php 变量是动态加载的
标签: php codeigniter codeigniter-2