![]() |
Scope of $data problems - 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: Scope of $data problems (/showthread.php?tid=52703) |
Scope of $data problems - El Forum - 06-22-2012 [eluser]A_funs[/eluser] I am trying to access data passed into $data in the constructor in other methods, $data is public so why isn't this working <?php class Company extends CI_Controller{ public $data; function __construct(){ parent::__construct(); $data['something'] = 'something'; } function submit_addcompany(){ $data['test_2'] = "something else"; //HOPEFULLY $data will contain both 'something and 'something else'; } } } Scope of $data problems - El Forum - 06-22-2012 [eluser]PhilTem[/eluser] There's a difference in Code: $data which you seem to not be totally clear about. If you want to pass data between methods you either pass them as arguments or as class-attributes. Scope of $data problems - El Forum - 06-22-2012 [eluser]A_funs[/eluser] Thanks, So can you explain the difference a bit, after reading your response I tried this and got some errors... <?php class test extends CI_Controller{ function __construct(){ $data->name = "Alex"; } function index(){ echo $data->name; } } Scope of $data problems - El Forum - 06-22-2012 [eluser]A_funs[/eluser] sorry that last didn't make sense, this is still giving errors too... <?php class test extends CI_Controller{ function __construct(){ $data['name'] = "Alex"; } function index(){ echo $this->$data['name']; } } Scope of $data problems - El Forum - 06-22-2012 [eluser]A_funs[/eluser] OK got it... it was the dollar sign.. Thanks for your help Scope of $data problems - El Forum - 06-22-2012 [eluser]CroNiX[/eluser] For more info on how to do OOP in php, try checking out the manual. http://php.net/manual/en/language.oop5.php. If you don't understand OOP, you will have a harder time with CI. These are concepts you should really know before using CI to get the most of it. Scope of $data problems - El Forum - 06-22-2012 [eluser]A_funs[/eluser] Thanks CroNIX, I am getting pretty close to having what I believe to be an adequate understanding of OO principles, This one distinction between properties and variables seems to be escaping me though. If within my object I declare a public variable like "public $testdata" am I not making that variable available to all methods? Or is what I am really doing declaring a public property, which if I want to use across methods, I need to refer to as such with $this->testdata? thanks again Scope of $data problems - El Forum - 06-22-2012 [eluser]CroNiX[/eluser] Class variables, aka properties, are accessible only when using $this, as $this refers to the entire object, not the individual method. So you need to use $this to access individual class properties throughout the class. Code: class Someclass extends CI_Controller { In the above, when the class is instantiated, it sets the property, $data and echos it. 'my property data' If you go to /someclass/some_function: -goes through constructor first, sets property $data and echos it -sets local variable $data, sets property $data and echoes them both 'my property data' 'test' 'other data' If you go to /someclass/another_function -goes through constructor first, sets property $data and echos it -echos property $data, again -produces error that variable $data is undefined as it's only available within some_function where it was declared as a local variable. 'my property data' 'my property data' error, $data is undefined Hope that helps a bit. There is a difference between class variables and local variables, and you have to access them as such. |