thinkPHP自定義類實現(xiàn)方法詳解
本文實例講述了thinkPHP自定義類實現(xiàn)方法。分享給大家供大家參考,具體如下:
1.通過Model調(diào)用
<?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,否則會因為表不存在而報錯。
調(diào)用,
$Api = D('Api'); $Api->test();
調(diào)用確實方便,但是總感覺有點不合理。這個D畢竟是操作數(shù)據(jù)庫的。
2.通過引入類實現(xiàn),把類放到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)); } } ?>
調(diào)用
import("@.ORG.Api.Integral"); $integralApi = new Integral(); $integralApi->test();
配置一下,自動加載
'APP_AUTOLOAD_PATH' => '@.ORG,@.ORG.Api',
這樣調(diào)用就方便了不管Api文件夾下有多少類,都會自動加載,不需要單個引用import("@.ORG.Api.Integral")了。
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《smarty模板入門基礎(chǔ)教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP基于yii框架實現(xiàn)生成ICO圖標(biāo)
本文給大家分享了2則php生成ICO圖標(biāo)的代碼,第一款是基于YII框架實現(xiàn)的,第二款是純php編寫,都非常的不錯,有需要的小伙伴可以參考下2015-11-11Zend Framework框架的zend_cache緩存使用方法(zend框架)
這篇文章主要介紹了Zend_Cache文件緩存的基本操作,簡單的示例,,需要的朋友可以參考下2014-03-03ZendFramework框架實現(xiàn)連接兩個或多個數(shù)據(jù)庫的方法
這篇文章主要介紹了ZendFramework框架實現(xiàn)連接兩個或多個數(shù)據(jù)庫的方法,涉及ZendFramework框架配置文件與數(shù)據(jù)庫操作相關(guān)技巧,需要的朋友可以參考下2016-12-12THINKPHP3.2使用soap連接webservice的解決方法
這篇文章主要為大家詳細(xì)介紹了THINKPHP3.2使用soap連接webservice的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12PHP+FastCGI+Nginx配置PHP運行環(huán)境
Nginx不支持對外部程序的調(diào)用,所以必須通過FastCGI接口實現(xiàn)對外部程序的調(diào)用從而實現(xiàn)對客戶端動態(tài)頁面請求的處理。2014-08-08