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

Detailed explanation of thinkPHP custom class implementation methods

墨辰丷
Release: 2023-03-28 12:46:01
Original
1655 people have browsed it

This article mainly introduces the implementation methods of thinkPHP custom classes, and analyzes the definition and usage skills of thinkPHP custom model classes in the form of examples. Friends in need can refer to the following

The examples in this article describe the customization of thinkPHP Define class implementation methods. Share it with everyone for your reference, the details are as follows:

1. Call

<?php
/**
 * 积分模型 api接口
 */
class ApiModel{
  private $url = &#39;http://js.yunlutong.com/Customer/Interface&#39;;
  public function test() {
    $post_data[&#39;action&#39;]    = &#39;sadf&#39;;
    $post_data[&#39;callback&#39;]   = &#39;?&#39;;
    $res = request_post($this->url, $post_data);
    $firstChar = substr($res,0,1);
    if ($firstChar ==&#39;?&#39;) {
      $res = substr($res,2);
      $res = substr($res,0,strlen($res)-1);
    } elseif($firstChar == &#39;(&#39;) {
      $res = substr($res,1);
      $res = substr($res,0,strlen($res)-1);
    }
    dump(json_decode($res,true));
  }
}
Copy after login

through Model without inheriting Model, otherwise the table will not exist And report an error.

Calling,

$Api = D(&#39;Api&#39;);
$Api->test();
Copy after login

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

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

<?php
class Integral{
  private $url = &#39;http://js.yunlutong.com/Customer/Interface&#39;;
  public function test() {
    $post_data[&#39;action&#39;]    = &#39;sadf&#39;;
    $post_data[&#39;callback&#39;]   = &#39;?&#39;;
    $res = request_post($this->url, $post_data);
    $firstChar = substr($res,0,1);
    if ($firstChar ==&#39;?&#39;) {
      $res = substr($res,2);
      $res = substr($res,0,strlen($res)-1);
    } elseif($firstChar == &#39;(&#39;) {
      $res = substr($res,1);
      $res = substr($res,0,strlen($res)-1);
    }
    dump($res);
    dump(json_decode($res,true));
  }
}
?>
Copy after login

Call

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

Configure it and load it automatically

&#39;APP_AUTOLOAD_PATH&#39;   => &#39;@.ORG,@.ORG.Api&#39;,
Copy after login

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

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations: Detailed explanation of the usage of str_pad() function in

php

phpMethod to implement PDO-based preprocessing

detailed explanation of bind_param() function usage in php

The above is the detailed content of Detailed explanation of thinkPHP custom class implementation methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template