CodeIgniter Forums
passing data from __construct to functions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: passing data from __construct to functions (/showthread.php?tid=38748)



passing data from __construct to functions - El Forum - 02-17-2011

[eluser]Media Gearhead[/eluser]
So this code used to work for me in CI 1.7.* but now it is causing issues in CI 2

Worked in CI 1.7.x
Code:
class Error404 extends Controller {
    function __construct(){
        parent::Controller();
        $this->data = array('page_title','Page not found 404');
    }
    
    function index(){
        echo $this->data['page_title'];
    }
}

Doesn't work in CI 2
Code:
class Error404 extends CI_Controller {
    function __construct(){
        parent::__construct();
        $this->data = array('page_title','Page not found 404');
    }
    function index(){
        echo $this->data['page_title'];
    }
}

I am not sure what is happening that is different. I have tried changing parent::__construct(); to parent::Controller(); and parent::CI_Controller(); and every other combination I can think of.
I just want to be able to pass data from the construct to the other functions and I can't get the syntax right.


passing data from __construct to functions - El Forum - 02-17-2011

[eluser]Media Gearhead[/eluser]
So deleting the above post wouldn't help my post count at all...
I am going to go walk around the office and kick myself repeatedly. I figured out my obvious mistake.


passing data from __construct to functions - El Forum - 02-17-2011

[eluser]Jaketoolson[/eluser]
Your array is structured incorrectly. You set 2 values for the array rather than a key->value pair.
Code:
//from
$this->data = array('page_title','Page not found 404');
//to
$this->data = array('page_title' => 'Page not found 404');



passing data from __construct to functions - El Forum - 02-17-2011

[eluser]Media Gearhead[/eluser]
I had noticed that in my second post but thank you for the response regardless.


passing data from __construct to functions - El Forum - 02-17-2011

[eluser]InsiteFX[/eluser]
Code:
class Error404 extends CI_Controller {
  
    private $data = array();

    function __construct(){
        parent::__construct();
        $this->data = array('page_title','Page not found 404');
    }
    function index(){
        echo $this->data['page_title'];
    }
}

InsiteFX