CodeIgniter Forums
how declare variable inside constructor? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: how declare variable inside constructor? (/thread-22233.html)



how declare variable inside constructor? - El Forum - 09-02-2009

[eluser]ranjitbd[/eluser]
class Holiday extends Controller {

function Holiday()
{
parent::Controller();
$data = array(); // can i declare a variable like this in a constructor

}

function access_variable()
{
//want to access that variable like this
$data['name'] = 'ranjit'; // will it work?even i want to access this $data
//in all methods in the controller.
}
}
?>


how declare variable inside constructor? - El Forum - 09-02-2009

[eluser]Dam1an[/eluser]
You've been told to use code tags so many times now!
Look into using $this, it works in the scope of a class instead of just a function. I'll let you actually read up on this and figure something out for yourself

Oh, and one last thing, you seem to be asking "will this work" in a lot of your posts. Why not try it and find our, and then read up on it to understand why


how declare variable inside constructor? - El Forum - 09-02-2009

[eluser]WildPhoenix[/eluser]
Code:
class Holiday extends Controller {

  var $data;

  function Holiday()
  {
    parent::Controller();
  }

  function access_variable()
  {
        $this->data[‘name’] = ‘ranjit’;
  }

  function another_func()
  {
        $this->data[‘different’] = ‘content’;
  }
}



how declare variable inside constructor? - El Forum - 09-25-2009

[eluser]ranjitbd[/eluser]
[quote author="Bubblz" date="1251909743"]
Code:
class Holiday extends Controller {

  var $data;

  function Holiday()
  {
    parent::Controller();
  }

  function access_variable()
  {
        $this->data[‘name’] = ‘ranjit’;
  }

  function another_func()
  {
        $this->data[‘different’] = ‘content’;
  }
}
[/quote]
really thanks a lot...one more thing that is
Code:
// can i use
$data = array();
//instead of
var $data;



how declare variable inside constructor? - El Forum - 09-25-2009

[eluser]n0xie[/eluser]
Why don't you try it and find out...?


how declare variable inside constructor? - El Forum - 09-25-2009

[eluser]BrianDHall[/eluser]
One of the nice things about PHP is it isn't compiled - it takes mere seconds to change something and see if it works. PHP will very quickly let you know if you can declare a variable in a class outside of a function without using an explicit call to var.

If you try out an IDE such as Netbeans, PHPed, or Eclipse with PHP modules they have in-line syntax checkers which will also let you know if something works or not without even needing to refresh your browser.

While we really like helping, none of us like to be personal versions of documentation.

http://us2.php.net/manual/en/language.oop5.php


how declare variable inside constructor? - El Forum - 07-19-2011

[eluser]Syed Wasti[/eluser]
[quote author="Dam1an" date="1251893648"]You've been told to use code tags so many times now!
Look into using $this, it works in the scope of a class instead of just a function. I'll let you actually read up on this and figure something out for yourself

Oh, and one last thing, you seem to be asking "will this work" in a lot of your posts. Why not try it and find our, and then read up on it to understand why[/quote]

This is definitely not a way to respond on a forum.
Secondly, there is is very little or incomplete information regarding CI constructs and concepts in the documentation.

Do not be loose while commenting on my post.