Welcome Guest, Not a member yet? Register   Sign In
noob question
#1

[eluser]banjomacman[/eluser]
Can someone explain when to use

$variable

instead of

$this->varible

inside controller,models,libraries
#2

[eluser]ELRafael[/eluser]
i'll try!

see this example:
Code:
class Foo
{
  var $fruit;

  function Foo()
  {
    $this->fruit = 'banana';
  }
  
  function print_fruit()
  {
    $fruit = 'apple';
    echo $fruit.' and '.$this->fruit;
  }
}

$bar = new Foo();
$bar->print_fuit();   //Show me the string "apple and banana"

When i call print_fruit method, i have 2 variables. One for all class ($this->fruit) and another ONLY for print_fruit method ($fruit).

Can you see the difference?
#3

[eluser]xwero[/eluser]
$this is a reference to the class a variable or a method belongs to.
Code:
class base
{
    public $var;
    function foo(){}
}
class extended extends base
{
    function bar(){ $this->var = $this->foo; }
}
The example shows the inheritance of variables and methods using $this.
#4

[eluser]banjomacman[/eluser]
So if I want to access my variable from anywhere in the class I need to use $this->varName, but if I'm only working with it in a method and returning it I only need $varName. Correct? It's a scope thing. Sounds like in most of my controllers,models,libraries I won't need $this->varName. Thanks. Mike.




Theme © iAndrew 2016 - Forum software by © MyBB