0

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.

1
  • $this->$komikModel = new KomikModel(); needs to be $this->komikModel = new KomikModel();. Also you need to define private variable $komikModel as well Commented Sep 8, 2021 at 7:32

1 Answer 1

1

change

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

to

$this->komikModel = new KomikModel();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.