CodeIgniter Forums
Newbie Help w/ Constructors - 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: Newbie Help w/ Constructors (/showthread.php?tid=35551)



Newbie Help w/ Constructors - El Forum - 11-02-2010

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

}



Newbie Help w/ Constructors - El Forum - 11-02-2010

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

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

}



Newbie Help w/ Constructors - El Forum - 11-02-2010

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

}



Newbie Help w/ Constructors - El Forum - 11-02-2010

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

InsiteFX


Newbie Help w/ Constructors - El Forum - 11-02-2010

[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