


Detailed explanation of thinkPHP custom class implementation method_php example
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
<?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)); } }
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
<?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)); } } ?>
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

There are several versions of thinkphp

Which one is better, laravel or thinkphp?

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks

ThinkPHP6 backend management system development: realizing backend functions
