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

[eluser]ecsyle31[/eluser]
If you want to make $this->data['name'] available in your methods you should define it in the controllers constructor. The other() function has no idea what is going on in index() because you are not calling those functions from each other.

This isn't a scope issue so much as a misunderstanding of OOP. http://php.net/manual/en/language.oop5.php

Code:
class Main extends CI_Controller {

        var $data;
      
        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"
        }
}

When you access http://yoursite/main/other it will echo "default_value", accessing http://yoursite.com/main/index will echo "hello".


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