John Hoffman
John Hoffman

Reputation: 18627

Why can I not pass values initialized in the constructor of my Code Igniter controller?

I have this controller in Code Igniter application. A value is initialized in the constructor.

class Cat extends CI_Controller {
    private $data = array();

    public function __construct() {
        parent::__construct();
        $this->data['sound'] = "meow";
    }                                 
    public function index() {
        $this->load->view('myCatPage', $data); 
    }
}

The view "views/myCatPage.php" looks like this. It is simple.

<?= $sound ?>

Why does PHP note this error?

Message: Undefined variable: sound

I thought I sent this variable as a key in the array ($data) I sent into the view. I have tried

$this->load->view('myCatPage', $this->data);

but that strangely fails too.

Upvotes: 3

Views: 1222

Answers (1)

The Alpha
The Alpha

Reputation: 146219

class Cat extends CI_Controller {
    var $data = array();
    public function __construct() {
        parent::__construct();
        $this->data['sound'] = "meow";
    }                                 
    public function index() {
        $this->load->view('myCatPage', $this->data); 
    }
}

Upvotes: 9

Related Questions