這篇文章主要介紹了thinkPHP自訂類別實作方法,結合實例形式分析了thinkPHP自訂模型類別的定義與使用技巧,需要的朋友可以參考下
本文實例講述了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();配置一下,自動載入
php中str_pad()函數用法詳解 php實作基於PDO的預處理的方法 以上是thinkPHP自訂類別實作方法詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!'APP_AUTOLOAD_PATH' => '@.ORG,@.ORG.Api',
相關推薦: