Home > Mobile >  Codeigniter 4 doesnt declare models when put inside constructor
Codeigniter 4 doesnt declare models when put inside constructor

Time:09-17

I have a problem when i want to declare models only once, i make it like this

  <?php

namespace App\Controllers;

use App\Models\KomikModel;

class Komik extends BaseController
{
    protected $komikModel;
    public function __construct()
    {
        $this->$komikModel = new KomikModel();
    }

    public function index()
    {
        $komik = $this->komikModel->findAll();
        $data = [
            'title' => 'Daftar Komik',
            'komik' => $komik,
        ];

        return view('komik/index', $data);
    }

}

but its get error saying undefined variable $komikModel on this line 12 which is this

public function __construct()
{
    $this->$komikModel = new KomikModel();
}

how do i fix this ? or maybe you can suggest with better practice than this one.

CodePudding user response:

change

$this->$komikModel = new KomikModel(); 

to

$this->komikModel = new KomikModel();
  • Related