Navaneetha Krishnan K Navaneetha Krishnan K - 6 months ago 12
PHP Question

cannot able to access the variable from parent constructor

Hi friends I'm new to CodeIgniter, I've downloaded version 3.04 and in controller page i've created parent constructor .

Controller :

class Vote extends CI_Controller {
public function construct(){
parent::__construct();
$this->data['theme'] = 'admin';
}
public function index()
{
$this->data['page'] = 'login_page';
$this->load->vars($this->data);
$this->load->view($this->data['theme'].'/template');
}


Error :


A PHP Error was encountered

Severity: Notice

Message: Undefined index: theme

Filename: controllers/vote.php

Line Number: 30

Backtrace:

File: C:\xampp\htdocs\poll\application\controllers\vote.php Line: 30
Function: _error_handler

File: C:\xampp\htdocs\poll\index.php Line: 292 Function: require_once


Why do I get an error like this I have added $autoload['helper'] = array('url'); also.

Answer

First create a file called MY_Controller.php under application/core folder. In this file, you should create a class. E.g:

class Some_class extends CI_Controller {
public function __construct() {
parent::__construct();
$this->data['theme'] = 'admin';
 }
}

Than edit your Controller like below:

class Vote extends Some_class {
public function construct(){
parent::__construct();
}
public function index()
{        
$this->data['page'] = 'login_page';
$this->load->vars($this->data);
$this->load->view($this->data['theme'].'/template');
}