• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how declare variable inside constructor?

#1
[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.
}
}
?>

#2
[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

#3
[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’;
  }
}

#4
[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;

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

#6
[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

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.