【问题标题】:Codeigniter 4 constructor, not able to use data in other functionsCodeigniter 4 构造函数,无法在其他函数中使用数据
【发布时间】:2020-06-06 16:50:04
【问题描述】:

自从我在 CI 中使用构造函数以来已经有一段时间了。我查看了 CI4 的用户指南,构造函数似乎与 CI3 有点不同。我复制了代码并试用了它,但我收到一条错误消息:Cannot call constructor

    public function __construct(...$params)
    {
        parent::__construct(...$params);

        $model = new ShopModel();

        $findAll = [
            'shop' => $model->table('shop')->where('brand_name_slug', 'hugo-boss')->findAll()
        ];
    }

从这里,我在网上搜索并看到一个类似的帖子,建议完全删除parent::__construct(...$params); 行。当我这样做时,页面会加载 - 但是当我尝试在 Controller 函数中调用它时 $findAll 数组为 NULL 我需要它:

    public function brand_name($brand_name_slug)
    {
        $model = new ShopModel();

        var_dump($findAll['shop']);

        $data = [
            'shop' => $findAll['shop'],
        ];

        echo view('templates/header', $data);
        echo view('shop/view', $data);
        echo view('templates/footer', $data);
    }

非常感谢您的建议或指点!

【问题讨论】:

  • 你需要阅读 PHP 类。
  • 所以很明显,我可能会为我正在尝试做的事情向构造函数发出错误的键。我想要做的是将数据库查询$model->table('shop')->where('brand_name_slug', 'hugo-boss')->findAll() 定义为类内其他地方的变量($var),然后在函数控制器内brand_name 像这样调用它:data['shop'] = $var 我有什么解决方案想做什么? @TimBrownlaw

标签: codeigniter constructor controller construct codeigniter-4


【解决方案1】:

嗯,这是另一个答案,有点不客气。

一个类有 属性(类中的变量,对所有使用 $this 的方法可见,您已阅读过...)和

方法(函数)

<?php
namespace App\Controllers;
use App\Controllers\BaseController; // Just guessing here
use App\Models\ShopModel; // Just guessing here

class YourClass extends BaseController {

    // Declare a Property - Its a PHP Class thing...

    protected $findAll; // Bad Name - What are we "Finding"?

    public function __construct()
    {
        // parent::__construct(); // BaseController has no Constructor

        $model = new ShopModel(); // I am guessing this is in your App\Controllers Folder.

        // Assign the model result to the badly named Class Property
        $this->findAll = [
            'shop' => $model->table('shop')->where('brand_name_slug', 'hugo-boss')->findAll()
        ];
    }


    public function brand_name($brand_name_slug)
    {
        var_dump($this->findAll['shop']);

        $data = [
            'shop' => $this->findAll['shop'],
        ];

        // Uses default App\Views\?
        echo view('templates/header', $data);
        echo view('shop/view', $data);
        echo view('templates/footer', $data);
    }
}

要了解 publicprotectedprivate$this 关键字的作用 - 阅读 PHP上课……可以的,不难。

【讨论】:

  • 舌头和脸颊当之无愧 - 然而,它起作用了 - 你刚刚帮助我了解我哪里出错了。需要更多的研究!谢谢!
【解决方案2】:

$findall 应该是一个类变量(在类内部但在所有方法之外声明)并使用 this 关键字访问/修改,如下所示:

Class Your_class_name{

 private $findAll;  //This can be accessed by all class methods

 public function __construct()
    {
        parent::__construct();

        $model = new ShopModel(); //Consider using CI's way of initialising models

        $this->findAll = [
            'shop' => $model->table('shop')->where('brand_name_slug', 'hugo-boss')->findAll()
        ]; //use the $this keyword to access class variables
    }


public function brand_name($brand_name_slug)
    {
        ...

        $data = [
            'shop' => $this->findAll['shop'], //use this keyword
        ];

        ....
    }

【讨论】:

  • 只是好奇什么是“CI 初始化模型的方式”。
猜你喜欢
  • 1970-01-01
  • 2017-04-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
  • 2021-06-15
  • 1970-01-01
相关资源
最近更新 更多