thinkPHP自定義類(lèi)實(shí)現(xiàn)方法詳解
本文實(shí)例講述了thinkPHP自定義類(lèi)實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
1.通過(guò)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)); } }
沒(méi)有繼承Model,否則會(huì)因?yàn)楸聿淮嬖诙鴪?bào)錯(cuò)。
調(diào)用,
$Api = D('Api'); $Api->test();
調(diào)用確實(shí)方便,但是總感覺(jué)有點(diǎn)不合理。這個(gè)D畢竟是操作數(shù)據(jù)庫(kù)的。
2.通過(guò)引入類(lèi)實(shí)現(xiàn),把類(lèi)放到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();
配置一下,自動(dòng)加載
'APP_AUTOLOAD_PATH' => '@.ORG,@.ORG.Api',
這樣調(diào)用就方便了不管Api文件夾下有多少類(lèi),都會(huì)自動(dòng)加載,不需要單個(gè)引用import("@.ORG.Api.Integral")了。
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《ThinkPHP入門(mén)教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門(mén)教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《smarty模板入門(mén)基礎(chǔ)教程》及《PHP模板技術(shù)總結(jié)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
- Thinkphp5.0自動(dòng)生成模塊及目錄的方法詳解
- 淺談ThinkPHP5.0版本和ThinkPHP3.2版本的區(qū)別
- thinkPHP5.0框架獨(dú)立配置與動(dòng)態(tài)配置方法
- thinkPHP5.0框架引入Traits功能實(shí)例分析
- thinkPHP5.0框架整體架構(gòu)總覽【應(yīng)用,模塊,MVC,驅(qū)動(dòng),行為,命名空間等】
- 學(xué)習(xí)thinkphp5.0驗(yàn)證類(lèi)使用方法
- ThinkPHP控制器間實(shí)現(xiàn)相互調(diào)用的方法
- thinkphp模板繼承實(shí)例簡(jiǎn)述
- ThinkPHP5.0框架控制器繼承基類(lèi)和自定義類(lèi)示例
相關(guān)文章
PHP基于yii框架實(shí)現(xiàn)生成ICO圖標(biāo)
本文給大家分享了2則php生成ICO圖標(biāo)的代碼,第一款是基于YII框架實(shí)現(xiàn)的,第二款是純php編寫(xiě),都非常的不錯(cuò),有需要的小伙伴可以參考下2015-11-11Zend Framework框架的zend_cache緩存使用方法(zend框架)
這篇文章主要介紹了Zend_Cache文件緩存的基本操作,簡(jiǎn)單的示例,,需要的朋友可以參考下2014-03-03PHP對(duì)接微信公眾平臺(tái)消息接口開(kāi)發(fā)流程教程
這篇文章主要介紹了PHP對(duì)接微信公眾平臺(tái)消息接口開(kāi)發(fā)流程,如何使用PHP版接口操作公眾平臺(tái)消息,需要的朋友可以參考下2014-03-03PHP實(shí)現(xiàn)簡(jiǎn)易用戶登錄系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)簡(jiǎn)易用戶登錄系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07解決laravel5.4下的group by報(bào)錯(cuò)的問(wèn)題
今天小編就為大家分享一篇解決laravel5.4下的group by報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10ZendFramework框架實(shí)現(xiàn)連接兩個(gè)或多個(gè)數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了ZendFramework框架實(shí)現(xiàn)連接兩個(gè)或多個(gè)數(shù)據(jù)庫(kù)的方法,涉及ZendFramework框架配置文件與數(shù)據(jù)庫(kù)操作相關(guān)技巧,需要的朋友可以參考下2016-12-12THINKPHP3.2使用soap連接webservice的解決方法
這篇文章主要為大家詳細(xì)介紹了THINKPHP3.2使用soap連接webservice的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12PHP+FastCGI+Nginx配置PHP運(yùn)行環(huán)境
Nginx不支持對(duì)外部程序的調(diào)用,所以必須通過(guò)FastCGI接口實(shí)現(xiàn)對(duì)外部程序的調(diào)用從而實(shí)現(xiàn)對(duì)客戶端動(dòng)態(tài)頁(yè)面請(qǐng)求的處理。2014-08-08