thinkPHP自訂類實現方法詳解

來源:互聯網
上載者:User
這篇文章主要介紹了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下

<?phpclass 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")了。

以上就是本文的全部內容,希望對大家的學習有所協助。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.