工具扩展


工具类库扩展

把经常使用的功能封装成工具类,然后在项目中以工具对象形式使用是个不错的面向对象解决方案,工具类扩展步骤:

  1. 在Mb/tools目录下创建工具类文件,命名规则 : 工具类名称.php (如:test.php)
  2. 编写类文件的代码,如:
				
<?php
/**
 * test 类
 */
namespace Mb\tools; //注意命名空间
class test{
    public function hi(){
        echo 'PHP是世界上最好的语言';
    }
	
}
				
			

在控制器或视图内调用,如:

				
<?php
class indexController extends Mb{
    public function index(){
        $test = new Mb\tools\test();
        $test->hi();
    }
}
				
			

还可以通过框架方法函数进行调用

				
<?php
class indexController extends Mb{
    public function index(){
        $test = t('test');
        $test->hi();
    }
}