Welcome Guest, Not a member yet? Register   Sign In
passing data from __construct to functions
#1

[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.
#2

[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.
#3

[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');
#4

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

[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




Theme © iAndrew 2016 - Forum software by © MyBB