ThinkPHP custom class implementation methods, thinkphp details
This example describes how to implement thinkPHP custom classes. We will share this with you for your reference. The details are as follows:
1. Call with Model
<? Php/*** integral model api interface */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 ));}}
The Model is not inherited; otherwise, an error is returned because the table does not exist.
Call,
$Api = D('Api');$Api->test();
The call is really convenient, but it seems a bit unreasonable. After all, this D is used to operate databases.
2. Put the class under ORG by introducing the class implementation
<?phpclass 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 and automatically load
'APP_AUTOLOAD_PATH' => '@.ORG,@.ORG.Api',
This makes it easy to automatically load the Api regardless of the number of classes in the Api folder. You do not need to reference a single import ("@. ORG. Api. Integral.