《PHP編程:thinkPHP自定義類(lèi)實(shí)現(xiàn)方法詳解》要點(diǎn):
本文介紹了PHP編程:thinkPHP自定義類(lèi)實(shí)現(xiàn)方法詳解,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
相關(guān)主題:thinkphp教程
本文實(shí)例講述了thinkPHP自定義類(lèi)實(shí)現(xiàn)方法.分享給大家供大家參考,具體如下:PHP實(shí)戰(zhàn)
1.通過(guò)Model調(diào)用PHP實(shí)戰(zhàn)
<?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)); } }
沒(méi)有繼承Model,否則會(huì)因?yàn)楸聿淮嬖诙鴪?bào)錯(cuò).PHP實(shí)戰(zhàn)
調(diào)用,PHP實(shí)戰(zhàn)
$Api = D('Api'); $Api->test();
調(diào)用確實(shí)方便,但是總感覺(jué)有點(diǎn)不合理.這個(gè)D畢竟是操作數(shù)據(jù)庫(kù)的.PHP實(shí)戰(zhàn)
2.通過(guò)引入類(lèi)實(shí)現(xiàn),把類(lèi)放到ORG下PHP實(shí)戰(zhàn)
PHP實(shí)戰(zhàn)
<?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)); } } ?>
調(diào)用PHP實(shí)戰(zhàn)
import("@.ORG.Api.Integral"); $integralApi = new Integral(); $integralApi->test();
配置一下,自動(dòng)加載PHP實(shí)戰(zhàn)
'APP_AUTOLOAD_PATH' => '@.ORG,@.ORG.Api',
這樣調(diào)用就方便了不管Api文件夾下有多少類(lèi),都會(huì)自動(dòng)加載,不需要單個(gè)引用import("@.ORG.Api.Integral")了.PHP實(shí)戰(zhàn)
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《ThinkPHP入門(mén)教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門(mén)教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《smarty模板入門(mén)基礎(chǔ)教程》及《PHP模板技術(shù)總結(jié)》.PHP實(shí)戰(zhàn)
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助.PHP實(shí)戰(zhàn)
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/2544.html