爱问知识人 爱问教育 医院库

thinkPHP自定义类实现方法详解

首页

thinkPHP自定义类实现方法详解


        

提交回答
好评回答
  • 2023-03-08 04:30:09

    本文实例讲述了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")了。

    爱***

    2023-03-08 04:30:09

类似问题

换一换
  • 电脑/网络 相关知识

  • 电脑网络技术
  • 电脑网络

相关推荐

正在加载...
最新资料 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):