首页 > Web开发 > 详细

thinkphp5的控制器调用自身模块和调用其他模块的方法

时间:2018-07-14 21:21:45      阅读:352      评论:0      收藏:0      [点我收藏+]

以user为例,调用user.php的get_number()方法

一、不管是调用自身模块还是其他模块app\model\User.php写法不变

 1 <?php
 2 
 3 namespace app\index\model;
 4 use think\Model;
 5 
 6 class user extends Model
 7 {
 8     public function get_member(){
 9         return 3;
10     }
11 }

二、user控制器调用user模块(即控制器调用自身模块)

 1 <?php
 2 namespace app\index\controller;
 3 use think\Db;
 4 use think\Controller;
 5 use app\index\model\User as Users;
 6 class User extends Controller
 7 {
 8     public function wudi(){
 9         $user = new Users();
10         $user = $user -> get_member();
11         echo $user;
12     }
13 }

 

三、Index控制器调用user模块(即控制器调用其他模块)

<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use app\index\model\User;
class Index extends Controller
{
    public function wudi(){
        $user = new User();
        $user = $user -> get_member();
        echo $user;
    }
}

四、分析

控制器调用自身模块因为名字都是User,会出现报错。

为了防止这种情况,对User重命名就可以了

重命名语法是(User as 新名字)

 

thinkphp5的控制器调用自身模块和调用其他模块的方法

原文:https://www.cnblogs.com/piaobodewu/p/9310980.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!