simple scope issue |
[eluser]dmol[/eluser]
Hi all, I'm new to CI and indeed PHP classes and I'm having trouble with scope of variables. I want to define a variable that can be used throughout a controller. It should be straightforward but the solutions I've seen don't seem to work for me. I'm using CI version 2 and PHP version 5.1.6 <?php Below are two basic versions of the controller: This does not work: Code: class Main extends CI_Controller { Code: class Main extends CI_Controller {
[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 { When you access http://yoursite/main/other it will echo "default_value", accessing http://yoursite.com/main/index will echo "hello".
[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 { InsiteFX
[eluser]John_Betong[/eluser]
I was under the impression that with CI2 it was essential to use the following construct: Code: class Main extends CI_Controller {
[eluser]dmol[/eluser]
Thanks for the replies everybody: @rlindauer & @InsiteFX: Yes I need to do some reading on OOP. I've written a fair amount of stand-alone PHP code before but I do think the CI documentation should cover this information better though. |
Welcome Guest, Not a member yet? Register Sign In |