这是一个特殊的类, 简称常量类。我们建议放置错误码、错误内容、常量
当Constants类被加载时, 并且定义了属性 ErrDescription, Asf_Ensure类才能正常工作
可以使用PHP函数加载 include/include_once、require/require_once, 也可以使用框架提供的方法调用
Constants.php 文件, 默认路径: asf.root_path/application/(Constants.php OR constants.php)
如果 Bootstrap 类中使用了常量类, 请先调用方法 constants()
自定义路径方法, 设置配置项 asf.constants=/{$other_path}/(Constants.php OR constants.php)
使用 Asf_Ensure 类进行条件判断, 中断型输出在Constants文件里定义的错误码、错误内容
调用方法 constants 会加载 constants.php文件
<?php
class IndexService
{
public function indexAction()
{
Asf_Ensure::notNull(true, Constants::ERR_TEST_CODE);
}
}
class Constants
{
const ERR_TEST_CODE = 500;
public static $ErrDescription = array(
self::ERR_TEST_CODE => 'This is test default text'
);
}
$configs = array(
'asf' => array(
'root_path' => MODULE_PATH,
)
);
$handle = new Asf_Application($configs);
$handle->constants()->run();
调用方法 constants 会加载 constants.php文件
调用方法 bootstrap 会加载 bootstrap.php文件
<?php
class IndexService
{
public function indexAction()
{
return Constants::ERR_INDEX_SUCCESS;
}
}
class Bootstrap
{
public function _initHow()
{
var_dump(Asf_Loader::get('UserLogic')->getUser());
}
}
class Constants
{
const ERR_INDEX_SUCCESS = 'hello world';
}
class UserLogic
{
public function getUser()
{
return 'zhangsan';
}
}
$configs = array(
'asf' => array(
'root_path' => MODULE_PATH,
'constants' => MODULE_PATH . '/constants/constants.php',
)
);
$handle = new Asf_Application($configs);
$handle->constants()->bootstrap()->run();
默认类名首字母大写, 其余字母转换为小写, asf.root_path/library/Catconstants.php
<?php
class IndexService
{
public function indexAction()
{
return Catconstants::ERR_INDEX_SUCCESS;
}
}
class CatConstants
{
const ERR_INDEX_SUCCESS = 'hello world';
}