我想遵循的一件事是坚持最佳编码实践。其中之一是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
消息。
如何在配置文件中使用常量?
2条答案
按热度按时间wmvff8tz1#
如果你只需要定义常量。你可以在
./application/config/constants.php
文件中定义它们,它们在整个应用程序中都可用。sqyvllje2#
你需要自动加载Hero_attributes模型类。可能必须在自动加载配置文件之前完成。