CodeIgniter Forums

Full Version: Newbie Help w/ Constructors
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]mdgrech[/eluser]
I have some default variables I would like to set in my __construct for all my functions do use. Where am I going wrong here?


Code:
<?php
class Test extends Controller {
        
    function __construct()
    {
        parent::Controller();
        
        $this->message = "hello!";
    }
    
    
    function index()
    {        
        echo $message;    
    }

}

El Forum

[eluser]tonanbarbarian[/eluser]
Code:
<?php
class Test extends Controller {

    public $message;
        
    function __construct()
    {
        parent::Controller();
        
        $this->message = "hello!";
    }
    
    
    function index()
    {        
        echo $this->message;    
    }

}

El Forum

[eluser]mdgrech[/eluser]
There has got to be a better way. Doing it your way would result in code like below. Which defeats the whole purpose of setting some default values that all my functions will inherit.

Code:
<?php
class Test extends Controller {

    public $message;
        
    function __construct()
    {
        parent::Controller();
        
        $this->username = "Steve";
    }
    
    
    function index()
    {        
        $data['username'] = $this->username;  
        $this->load->view('foo', $data);
    }

   function account()
    {        
        $data['username'] = $this->username;
        $this->load->view('foo', $data);
    }

}

El Forum

[eluser]InsiteFX[/eluser]
Then read about the MY_Controller in the User Guide!

InsiteFX

El Forum

[eluser]lianpeng[/eluser]
Very nice post. This will help possible client to get a quality person.


cheap uggs and wedding dresses and jordan shoes sale