YII2自定义公共方法
目的:提高代码复用性,减少重复开发的时间,使逻辑代码简洁明了。
过程:
1.选择在common目录下建立我们的class类,即公共方法(我的yii框架是yii2-advanced版本)
2.编辑我们的公共方法,代码的模式如下
<?php namespace common\helpers; class InstructOrder{ public static function toStr($bytes){ $arr = array(); for($i=0;$i<strlen($bytes);$i++){ if(ord($bytes[$i])<=15 ){ $arr[$i]= '0'.dechex(ord($bytes[$i])); //解密 $arr[$i]= Decrypt($arr[$i]); }else{ $arr[$i]= dechex(ord($bytes[$i])); //解密 $arr[$i]= Decrypt($arr[$i]); } } return $arr; } }
3.在逻辑代码中调用我们公共方法
使用命名空间调用:
use \common\helpers\InstructOrder;//声明命名空间
InstructOrder::toStr('123');//调用
直接调用:
\common\helpers\InstructOrder::toStr('123');这样,就可以啦!