• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie Help w/ Constructors

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

}

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

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

}

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

}

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

InsiteFX

#5
[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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.