thinkphp使用自定義類方法


1.通過Model調用

<?php
/**
 * 積分模型 api接口
 */
class ApiModel{
    private $url = 'http://js.yunlutong.com/Customer/Interface';

	public function test() {
        $post_data['action']       = 'sadf';
        $post_data['callback']      = '?';
        $res = request_post($this->url, $post_data);
        $firstChar = substr($res,0,1);
        if ($firstChar =='?') {
            $res = substr($res,2);
            $res = substr($res,0,strlen($res)-1);
        } elseif($firstChar == '(') {
            $res = substr($res,1);
            $res = substr($res,0,strlen($res)-1);
        }
        dump(json_decode($res,true));
    }
}

沒有繼承Model,否則會因為表不存在而報錯。

調用,

$Api = D('Api');
$Api->test();

調用確實方便,但是總感覺有點不合理。這個D畢竟是操作數據庫的。

2.通過引入類實現,把類放到ORG下

<?php
class Integral{
	private $url = 'http://js.yunlutong.com/Customer/Interface';

	public function test() {
        $post_data['action']       = 'sadf';
        $post_data['callback']      = '?';
        $res = request_post($this->url, $post_data);
        $firstChar = substr($res,0,1);
        if ($firstChar =='?') {
            $res = substr($res,2);
            $res = substr($res,0,strlen($res)-1);
        } elseif($firstChar == '(') {
            $res = substr($res,1);
            $res = substr($res,0,strlen($res)-1);
        }
        dump($res);
        dump(json_decode($res,true));
    }

}
?>

調用

import("@.ORG.Api.Integral");
$integralApi = new Integral();
$integralApi->test();

配置一下,自動加載

'APP_AUTOLOAD_PATH'     => '@.ORG,@.ORG.Api',

這樣調用就方便了不管Api文件夾下有多少類,都會自動加載,不需要單個引用import("@.ORG.Api.Integral")了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM