thinkPHP自定義類實(shí)現(xiàn)方法詳解
本文實(shí)例講述了thinkPHP自定義類實(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));
}
}
沒有繼承Model,否則會(huì)因?yàn)楸聿淮嬖诙鴪?bào)錯(cuò)。
調(diào)用,
$Api = D('Api');
$Api->test();
調(diào)用確實(shí)方便,但是總感覺有點(diǎn)不合理。這個(gè)D畢竟是操作數(shù)據(jù)庫(kù)的。
2.通過(guò)引入類實(shí)現(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();
配置一下,自動(dòng)加載
'APP_AUTOLOAD_PATH' => '@.ORG,@.ORG.Api',
這樣調(diào)用就方便了不管Api文件夾下有多少類,都會(huì)自動(dòng)加載,不需要單個(gè)引用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é)》。
希望本文所述對(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)證類使用方法
- ThinkPHP控制器間實(shí)現(xiàn)相互調(diào)用的方法
- thinkphp模板繼承實(shí)例簡(jiǎn)述
- ThinkPHP5.0框架控制器繼承基類和自定義類示例
相關(guān)文章
destoon實(shí)現(xiàn)資訊信息前面調(diào)用它所屬分類的方法
這篇文章主要介紹了destoon實(shí)現(xiàn)資訊信息前面調(diào)用它所屬分類的方法,在模板制作中非常實(shí)用,需要的朋友可以參考下2014-07-07
ThinkPHP防止重復(fù)提交表單的方法實(shí)例分析
這篇文章主要介紹了ThinkPHP防止重復(fù)提交表單的方法,結(jié)合實(shí)例形式分析了thinkPHP防止重復(fù)提交表單的各種常見操作技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2018-05-05
windows中為php安裝mongodb與memcache
這篇文章主要介紹了windows中為php安裝mongodb與memcache的方法,十分的詳盡,需要的朋友可以參考下2015-01-01
php 字符串中的\n換行符無(wú)效、不能換行的解決方法
這篇文章主要介紹了php 字符串中的換行符無(wú)效、不能換行的解決方法,實(shí)際上是PHP的雙引號(hào)和單引號(hào)的使用問(wèn)題,需要的朋友可以參考下2014-04-04

