Why can't I pass the values ​​initialized in the constructor of my Code Igniter controller?

I have this controller in a Code Igniter app. The 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's simple.

<?= $sound ?>

      

Why is PHP flagging this error?

Message: Undefined variable: sound

      

I thought I sent this variable as a key in array ( $data

) which I sent to the view. I tried

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

      

but this strangely fails too.

+3


source to share


1 answer


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); 
    }
}

      



+9


source







All Articles