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

[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 {

        var $data;  //I've also tried 'private $data' without success
      
        function Main()
                {
                parent::__construct();
                }
        function index()
                {   $this->data['name'] = "hello"; }
        function other()
                {
                 echo $this->data['name']; //fails. ['name'] is empty
                }
}
In the following version, the variable is visible in other functions but I think it's structure is causing problems when redirecting. I also don't know why an empty index function needs to be defined.
Code:
class Main extends CI_Controller {

        var $data;
      
        function Main()
                {
                parent::__construct();
                $this->data['name'] = "hello";
                }
        function other()
                {
                 echo $this->data['name']; //works. ['name'] contains hello
                }
        function index() //needs to be defined for some reason
                {
                }
}
#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".
#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
#4

[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 {

       function __construct() /* and NOT function Main() */
       {
            parent::__construct();
            // Your own constructor code
       }
}
 
 
 
#5

[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.




Theme © iAndrew 2016 - Forum software by © MyBB