我们假设全局性的公共类文件位于common/components/Helper.php,其内容如下:
|
1
2
3
4
5
6
7
8
9
|
<?phpnamespace common\components;class Helper{ public function checkedMobile ($mobile) { return $mobile; }} |
我们打开common\config\main.php文件对components选项进行如下配置:
|
1
2
3
4
5
6
7
|
‘components‘ => [ // other code... ‘helper‘ => [ ‘class‘ => ‘common\components\Helper‘, ‘property‘ => ‘123‘, ],], |
接着你只需要简单的使用
|
1
2
|
var_dump(Yii::$app->helper->checkedMobile(‘186xxx‘));//显示186xxxvar_dump(Yii::$app->helper->property);//显示123
|
原文:http://www.cnblogs.com/longzhankunlun/p/6261416.html