Welcome Guest, Not a member yet? Register   Sign In
simple scope issue
#3

[eluser]InsiteFX[/eluser]
Code:
I also don’t know why an empty index function needs to be defined.

This is because when you call a Controller it looks for the index method, which is the Controllers default method!

And this should be specified as this:
Code:
class Main extends CI_Controller {

        // should be this!
        private $data = array();
      
        function Main()
        {
                parent::__construct();
                $this->data['name'] = "default_value"; // the constructor is always called first, before your method is called. So $this->data['name'] will always be set.
        }

        function index()
        {  
                 $this->data['name'] = "hello";
                 echo $this->data['name']; // echos "hello"
        }

        function other()
        {
                 echo $this->data['name']; // echos "default_value"
        }
}

InsiteFX


Messages In This Thread
simple scope issue - by El Forum - 02-25-2011, 11:35 AM
simple scope issue - by El Forum - 02-25-2011, 11:56 AM
simple scope issue - by El Forum - 02-25-2011, 01:15 PM
simple scope issue - by El Forum - 02-26-2011, 02:32 AM
simple scope issue - by El Forum - 02-26-2011, 03:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB