thinkPHP5.0 用_initialize()替換__construct進行初始化


使用Loader:

<?php

namespace app\index\controller;

use think\Controller;

use think\Loader;

class Login extends Controller

{

  public function __construct()

  {

    parent::__construct();

    //data數據表的名字

    $this->data=Loader::model('data');

  }

  public function test()

  {

    $res=$this->data->getMenu();

    dump($res);

  }

}

使用 _initialize 代替__construct ,不同版本的tp5有時候_initialize不帶下划線

<?php

namespace app\index\controller;

use think\Controller;

use think\Loader;

class Login extends Controller

{

  public function _initialize()

  {

    //parent::__construct();

    //data數據表的名字

    $this->data=Loader::model('data');

  }

  public function test()

  {

    $res=$this->data->getMenu();

    dump($res);

  }

}

//model模型里面的Data.php

<?php

namespace app\index\model;

use think\Db;

use think\Model;

class Data extends Model

{

  protected $table='data';

  public function getMenu()

  {

    $result=Db::name($this->table)->select();

    return $result;

  }

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM