如何在thinkPHP中自定义类

  介绍

如何在thinkPHP中自定义类?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

1。通过模型调用

& lt; php ?/* *   ,*积分模型,api接口   ,*/class  ApiModel {   private 才能;url 美元;=,& # 39;http://js.yunlutong.com/Customer/Interface& # 39;;   public 才能;function 测试(),{   ,,,美元post_data[& # 39;行动# 39;),,,,=,& # 39;sadf& # 39;;   ,,,美元post_data[& # 39;调# 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);   ,,,}   ,,,转储(json_decode (res美元,真的));   ,,}   }

没有继承模型,否则会因为表不存在而报的错。

调用,

Api 美元;=,D (& # 39; api # 39;);   测试()Api→美元;

调用确实方便,但是总感觉有点不合理。这个D毕竟是操作数据库的。

2。通过引入类实现,把类放到ORG下

如何在thinkPHP中自定义类

& lt; php ?   class 积分{   private 才能;url 美元;=,& # 39;http://js.yunlutong.com/Customer/Interface& # 39;;   public 才能;function 测试(),{   ,,,美元post_data[& # 39;行动# 39;),,,,=,& # 39;sadf& # 39;;   ,,,美元post_data[& # 39;调# 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);   ,,,}   ,,,转储(res);   ,,,转储(json_decode (res美元,真的));   ,,}   }   祝辞;

调用

进口(“@.ORG.Api.Integral");   $ integralApi =, new 积分();   美元integralApi→测试();

配置一下,自动加载

& # 39; APP_AUTOLOAD_PATH& # 39;,,,=祝辞,& # 39;@.ORG, @.ORG.Api& # 39;,

这样调用就方便了不管Api文件夹下有多少类,都会自动加载,不需要单个引用进口(“@.ORG.Api.Integral")了。

如何在thinkPHP中自定义类