Home > Article > Backend Development > Detailed explanation of thinkPHP custom class implementation methods
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 = '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)); } }
through Model without inheriting Model, otherwise the table will not exist And report an error.
Calling,
$Api = D('Api'); $Api->test();
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 = '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 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
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!