search

Home  >  Q&A  >  body text

Thinkphp5 classes under the same page cannot be instantiated?

When using thinkphp5, on the same page

namespace app\index\model;
    use think\Model;
    // 此处$target='admins';$behaviour='nihao';
    class Firstrow{
        public function mainChoose($target,$behaviour){
            $tableName = $target.'model';
            $openGate = new $tableName();
            return $result = $openGate -> $behaviour();
        }
    }

    class adminsmodel extends Model{
        protected $table = 'admins';
        public function nihao(){
            $result = $this->select()->collection()->toArray();
            return $result;
        }
    }

But the system keeps prompting that the adminsmodel class cannot be found
I suspected that it was a namespace problem, but there was no solution for a while. Could you please answer my questions

迷茫迷茫2820 days ago502

reply all(1)I'll reply

  • PHPz

    PHPz2017-05-24 11:32:42

    正常来说你在另外写个model
    然后引用就行
    use app/index/mode/adminsmodel;
    new adminsmodel;
    
    如果你非要这么任性
    使用绝对路径 
    new app\index\model\adminsmodel 
    

    reply
    0
  • Cancelreply