Skip to content
Advertisement

Codeigniter 4 doesnt declare models when put inside constructor

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

  <?php

namespace AppControllers;

use AppModelsKomikModel;

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.

Advertisement

Answer

change

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

to

$this->komikModel = new KomikModel();
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement