Home >Backend Development >PHP Tutorial >Detailed explanation of thinkPHP custom class implementation method_php example

Detailed explanation of thinkPHP custom class implementation method_php example

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-12-05 13:28:251187browse

The example in this article describes the implementation method of thinkPHP custom class. Share it with everyone for your reference, the details are as follows:

1. Call through Model

<&#63;php
/**
 * 积分模型 api接口
 */
class ApiModel{
  private $url = 'http://js.yunlutong.com/Customer/Interface';
  public function test() {
    $post_data['action']    = 'sadf';
    $post_data['callback']   = '&#63;';
    $res = request_post($this->url, $post_data);
    $firstChar = substr($res,0,1);
    if ($firstChar =='&#63;') {
      $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));
  }
}

Do not inherit Model, otherwise an error will be reported because the table does not exist.

call,

$Api = D('Api');
$Api->test();

The call is indeed convenient, but it always feels a bit unreasonable. After all, this D operates the database.

2. Implement it by introducing classes and put the classes under ORG

<&#63;php
class Integral{
  private $url = 'http://js.yunlutong.com/Customer/Interface';
  public function test() {
    $post_data['action']    = 'sadf';
    $post_data['callback']   = '&#63;';
    $res = request_post($this->url, $post_data);
    $firstChar = substr($res,0,1);
    if ($firstChar =='&#63;') {
      $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));
  }
}
&#63;>

Call

import("@.ORG.Api.Integral");
$integralApi = new Integral();
$integralApi->test();

Configure it and load it automatically

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

This makes it convenient to call. No matter how many classes there are in the Api folder, they will be loaded automatically. There is no need to import ("@.ORG.Api.Integral") a single reference.

Readers who are interested in more thinkPHP-related content can check out the special topics on this site: "Introduction to ThinkPHP Tutorial", "Summary of thinkPHP Template Operation Skills", "Summary of Common Methods of ThinkPHP", "Introduction to Codeigniter Tutorial", "CI (CodeIgniter) Framework" Advanced Tutorial", "Basic Tutorial for Getting Started with Smarty Templates" and "Summary of PHP Template Technology".

I hope that what is described in this article will be helpful to everyone’s PHP program design based on the ThinkPHP framework.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn