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
You’re calling
$this->StudentModel = new Student_model();
. This error suggests the variable$StudentModel
doesn’t exist in either this class or the parentBaseController
. If it does exist inBaseController
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)
OR
Basically you can’t call
$this->{variable}
if ${variable} doesn’t exist or the class doesn’t know about it (private in the parent).