Welcome Guest, Not a member yet? Register   Sign In
var $varible in model
#1

[eluser]Kemik[/eluser]
Hello,

I know in other coding languages you have to declare variables and I noticed in some model examples the variables are decleared at the top of the model. Can anyone tell me why they have done this and if you need to var all the variables you use in the model?

Thanks.
#2

[eluser]xwero[/eluser]
if you declare a variable inside your class you can use it in all functions of the class.

Let say you have an id that is used by several functions.
Code:
class test
{
  var $id;
  // set id for the other functions
  function test()
  {
    $this->id = 1;
   }

  function name()
{
    $this->db->query('select name from persons where id=?',array($this->id));
  }

  function profession()
  {
    $this->db->query('select name from professions where id=?',array($this->id));
  }
}

It's not working code but i think you get the idea. Most of the time a get and set function is added to change the value of the variable.

Var is the php4 keyword, in php5 you can control the scope of the variable with public or private.

So you don't need to put var in front of the variables inside the functions.
#3

[eluser]Glen Swinfield[/eluser]
In php you do not have to declare variables in a class they can be created on the fly. This could be considered confusing if there is a list of declared variables but actually any number of other variables could exist.

Declaring variables does make it easier for someone to see at a glance what the most useful variables might be though. For example if you have a books class some declared vars might be "author_name", "ISBN_code" - it just makes it easier to see at a glance what these attributes are called in the class - but don't rely on it to be a definitive list of variables.
#4

[eluser]xwero[/eluser]
Ok learned something new today Smile




Theme © iAndrew 2016 - Forum software by © MyBB