thinkPHP自定义类实现方法详解


Posted in PHP onNovember 30, 2016

本文实例讲述了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下

thinkPHP自定义类实现方法详解

<?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();

配置一下,自动加载

'APP_AUTOLOAD_PATH'   => '@.ORG,@.ORG.Api',

这样调用就方便了不管Api文件夹下有多少类,都会自动加载,不需要单个引用import("@.ORG.Api.Integral")了。

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

PHP 相关文章推荐
由php if 想到的些问题
Mar 22 PHP
PHP 获取MSN好友列表的代码(2009-05-14测试通过)
Sep 09 PHP
PHP中冒号、endif、endwhile、endfor使用介绍
Apr 28 PHP
PHP Warning: PHP Startup: Unable to load dynamic library \ D:/php5/ext/php_mysqli.dll\
Jun 17 PHP
zend Framework中的Layout(模块化得布局)详解
Jun 28 PHP
php引用传值实例详解学习
Nov 06 PHP
Yii操作数据库的3种方法
Mar 11 PHP
PHP实现的带超时功能get_headers函数
Feb 10 PHP
CI框架中数据库操作函数$this-&gt;db-&gt;where()相关用法总结
May 17 PHP
解决laravel 5.1报错:No supported encrypter found的办法
Jun 07 PHP
php根据命令行参数生成配置文件详解
Mar 15 PHP
解决Laravel 不能创建 migration 的问题
Oct 09 PHP
php版阿里大于(阿里大鱼)短信发送实例详解
Nov 30 #PHP
php实现异步将远程链接上内容(图片或内容)写到本地的方法
Nov 30 #PHP
PHP实现的同步推荐操作API接口案例分析
Nov 30 #PHP
PHP实现多图上传(结合uploadify插件)思路分析
Nov 30 #PHP
PHP获取指定日期是星期几的实现方法
Nov 30 #PHP
php根据年月获取当月天数及日期数组的方法
Nov 30 #PHP
详解PHP处理密码的几种方式
Nov 30 #PHP
You might like
php清除和销毁session的方法分析
2015/03/19 PHP
php5.4以上版本GBK编码下htmlspecialchars输出为空问题解决方法汇总
2015/04/03 PHP
PHP获取毫秒级时间戳的方法
2015/04/15 PHP
PHP模板引擎Smarty中变量的使用方法示例
2016/04/11 PHP
php使用Header函数,PHP_AUTH_PW和PHP_AUTH_USER做用户验证
2016/05/04 PHP
WordPress中的shortcode短代码功能使用详解
2016/05/17 PHP
浅谈PHP中try{}catch{}的使用方法
2016/12/09 PHP
详解使用php调用微信接口上传永久素材
2017/04/11 PHP
document对象execCommand的command参数介绍
2006/08/01 Javascript
html读出文本文件内容
2007/01/22 Javascript
setTimeout和setInterval的浏览器兼容性分析
2007/02/27 Javascript
ajax 同步请求和异步请求的差异分析
2011/07/04 Javascript
jQuery实现带有动画效果的回到顶部和底部代码
2015/11/04 Javascript
jQuery解决IE6、7、8不能使用 JSON.stringify 函数的问题
2016/05/31 Javascript
javascript事件的传播基础实例讲解(35)
2017/02/14 Javascript
JS实现unicode和UTF-8之间的互相转换互转
2017/07/05 Javascript
node中Express 动态设置端口的方法
2017/08/04 Javascript
详解用Node.js写一个简单的命令行工具
2018/03/01 Javascript
angular 数据绑定之[]和{{}}的区别
2018/09/25 Javascript
一篇文章介绍redux、react-redux、redux-saga总结
2019/05/23 Javascript
Node.js API详解之 module模块用法实例分析
2020/05/13 Javascript
Element Collapse 折叠面板的使用方法
2020/07/26 Javascript
详解Python中DOM方法的动态性
2015/04/11 Python
python 根据正则表达式提取指定的内容实例详解
2016/12/04 Python
Python变量和字符串详解
2017/04/29 Python
python脚本作为Windows服务启动代码详解
2018/02/11 Python
详解Python Qt的窗体开发的基本操作
2019/07/14 Python
关于Python核心框架tornado的异步协程的2种方法详解
2019/08/28 Python
pycharm Tab键设置成4个空格的操作
2021/02/26 Python
css3让div随鼠标移动而抖动起来
2014/02/10 HTML / CSS
详解Html5微信支付爬坑之路
2018/07/24 HTML / CSS
简历中个人求职的自我评价模板
2013/11/29 职场文书
新教师个人总结
2015/02/06 职场文书
2016公司中秋节寄语
2015/12/07 职场文书
个人落户申请书怎么写?
2019/06/28 职场文书
Nginx + consul + upsync 完成动态负载均衡的方法详解
2021/03/31 Servers