zoukankan      html  css  js  c++  java
  • thinkPHP5.0 用_initialize()替换__construct进行初始化

    使用Loader:

    <?php

    namespace appindexcontroller;

    use thinkController;

    use thinkLoader;

    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 appindexcontroller;

    use thinkController;

    use thinkLoader;

    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 appindexmodel;

    use thinkDb;

    use thinkModel;

    class Data extends Model

    {

      protected $table='data';

      public function getMenu()

      {

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

        return $result;

      }

    }

  • 相关阅读:
    Python模块介绍及常见报错
    Vue入门及基本使用
    仿黑客帝国片头文字流星雨
    CSS3之flex布局演示
    京东商城延迟加载
    python第四次学习笔记
    python第二次学习笔记
    今天的第一个程序
    python第一次学习笔记(可能会有更新)
    ios导航条透明
  • 原文地址:https://www.cnblogs.com/ymdphp/p/10948237.html
Copyright © 2011-2022 走看看