skip to Main Content

Please help me fix this error –

Undefined property: AppControllersStudent::$StudentModel
Student_model Student::$StudentModel

I followed the video and this is the code for Student controller but got an error saying
"Undefined property: AppControllersStudent::$StudentModel"

<?php

namespace AppControllers;

use AppModelsStudent_model;

 
class Student extends BaseController
{
    
    public function __construct()
    {

        $this->StudentModel = new Student_model();
    }
   
    public function index()
    { 
        $data['student_info'] = $this ->StudentModel -> getStudentInfo();
        
        $data['Title'] = 'ITEC 222 Title';
        $data['Header'] = 'This is the new header';
        echo view('template/header', $data);
        echo view('student/index', $data);
        echo view('template/footer');
    }
}

2

Answers


  1. Undefined property: AppControllersStudent::$StudentModel
    

    You’re calling $this->StudentModel = new Student_model();. This error suggests the variable $StudentModel doesn’t exist in either this class or the parent BaseController. If it does exist in BaseController then it’s set to private and should be either public or protected. If it doesn’t exist in either class then you need to add it.

    Either (access modifier will depend on your use case)

    class BaseController
    {
        protected/public $StudentModel;
        ...
    }
    

    OR

    class class Student extends BaseController
    {
        protected/private/public $StudentModel;
        ...
    }
    

    Basically you can’t call $this->{variable} if ${variable} doesn’t exist or the class doesn’t know about it (private in the parent).

    Login or Signup to reply.
  2. Try this,
    public $StudentModel;
    public function __construct()
    {
    
        $this->StudentModel = new Student_model();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search