declaring class level variables in codeigniter

17,960

Solution 1

I solved the problem myself.

There are two things which I changed

protected $arr_CoreCountry = ('0'=>'uk', '1'=>'us');

was changed to

var $arr_CoreCountry = array(0=>'se', 1=>'fi',2=>'de'); 

and

foreach ($arr_CoreCountry as $key => $value)

was changed to

foreach ($this->arr_CoreCountry as $key => $value)

I was missing $this but when I put it there, it was still not working. When I changed protected to var, it worked.

Thanks everyone for your input...

Solution 2

This should work:

class OrderStats extends CI_Controller {

    protected $arr_CoreCountry = array('0'=>'uk', '1'=>'us'); 

    public function getOrderStats()
    {       
        $this->load->model('Orderstatsmodel', '', TRUE);

        //$data['result'] = $this->Testmodel->get_entries();
        foreach ($this->arr_CoreCountry as $key => $value)
        // etc

}

you are omitting the $this-> in your original code.

Edit Here was my test code ~

class Testing extends CI_Controller {

    protected $foo = array('test'=>'foo', 'bar'=>'baz');

    function index() {
        foreach($this->foo as $k => $v) {
            echo $k . ' = ' . $v . '<br />';
        }
    }
}

// outputs:
test = foo
bar = baz

perhaps you can post your syntax errors as they appear to be missing from your original post.

Share:
17,960
Syed
Author by

Syed

Hi, I work as software developer &amp; technical writer in a private IT firm in Stockholm, Sweden. I mainly work in PHP, MySQL, .NET, SQL Server, Ajax, Javascript.

Updated on June 30, 2022

Comments

  • Syed
    Syed almost 2 years

    I am new to CI and what I want to do is to have a class level variable (which e.g is an array). But it seems like CI, despite all high bragging, doesn't support this feature. Nothing has been mentioned in the user guide about it. There is a heading called private functions and variables but the text has been seemingly kept silent regarding variables.

    I want to have something like :

    class OrderStats extends CI_Controller {
    
    protected $arr_CoreCountry = ('0'=>'uk', '1'=>'us'); 
    
    public function __construct()
       {
    
            parent::__construct();
            // Your own constructor code
    
       }
    
       public function index()
    {
    
        $this->load->model('orders', '', TRUE);
        //$data['result'] = $this->Testmodel->get_entries();
        $data['result'] = $this->Testmodel->get_reports();
        $this->load->view('test', $data);
    
    }
    
    public function getOrderStats()
    {       
                $this->load->model('Orderstatsmodel', '', TRUE);
    
                //$data['result'] = $this->Testmodel->get_entries();
                foreach ($arr_CoreCountry as $key => $value)
                {
                    $data['result'] = $this->Orderstatsmodel->get_orderStats($key);
                }
      //                $data['result'] = $this->Orderstatsmodel->get_orderStats(0);
                $this->load->view('orderstats', $data);
    
    }
    

    Remember, when I declare $arr_CoreCountry variable at the place as it is in this post, I constantly see a syntax error message. When I place it some where inside any function then of course, it gets out of scope and I keep getting an error messag that $arr_CoreCountry is an undefined variable. So the question is where do I define it?

    Expect a quick response as half of my day has been wasted just because of this s*** from codeigniter.

  • Syed
    Syed almost 13 years
    Thanks but sorry it was typing mistake. I am declaring my array like you mentioned. But get undefined variable $arr_CoreCountry error message.
  • Goran
    Goran almost 13 years
    How about to try to declare the constant in constants.php file and use it in your controllers? Maybe that would help you? codeigniter.com/user_guide/general/styleguide.html#constants
  • Syed
    Syed almost 13 years
    OK... But where does the concept of class properties go then?
  • Goran
    Goran almost 13 years
    You need to understand that in MVC you do not have a real OOP incorporated. Controllers in Codeingiter are not supposed to have properties(obviously). They should just receive data, send them to Models for processing and then assign it to view. The only real OOP implementation here are models (they handle all the programming logic). They can and mostly should be classes with properties and methods (one example is model for each tabe in DB). Hope this helps...
  • Goran
    Goran almost 13 years
    Also Libraries in Codeigniter are pure OOP as well ;)
  • Syed
    Syed almost 13 years
    Well, BIG and MATURE languages in OOP like Java and .NET provide this flexibility in their respective frameworks to have class properties. Of course, in OOP, a class must be able to have and handle properties of its own or inherited ones. If CI is creating one of its own version of OOP concepts, then I wish them all the best and I now understand why is there so much criticism for it and why people go with other frameworks despite that they are slow and heavy. CI need to understand that MVC and OOP concepts are in practice in a much better implemented way than theirs.
  • Syed
    Syed almost 13 years
    But my question is still there. I know it is possible with CI and I need someone to help me out how to do it and where I am going wrong with this declaration thingy.
  • Syed
    Syed almost 13 years
    Yes, I omitted that from my post because it doesn't make any difference. The error still remains.
  • Ross
    Ross almost 13 years
    I disagree; I've just tested and unless I misunderstand, it works without issue.
  • Syed
    Syed almost 13 years
    Well the fact is that it wasn't working at my side. However, I solved the issue myself. Will post my answer shortly.
  • Syed
    Syed about 11 years
    Greatly disappointed with how CI has limitations in its OOP implementation and covering them under MVC shell (which is sort of lame at their end). Me and my team is dropping CI and will not continue developing stuff in it. Symfony is much sound and powerful framework (and giving developers so much power). Thanks everyone and good luck! :)
  • Goran
    Goran about 11 years
    I wish you the best of luck with Symfony, but I have switched myself to FuelPHP. It is the CI that should have been in the first place :)