Page 1 of 1

Why $this-> with properties???

Posted: Tue Oct 12, 2010 8:12 pm
by ATLChris
I am trying to grip the OOP concepts in PHP and am struggling with some areas.

One of which is, why do you have to use $this-> when declaring a property? It seems like sometimes I have to use $this->value and sometime I can just use $value. Why is that, what are the differences and when do I have to append with $this->. Is it just with properties that are being passed into the method? i.e $ThisClass->ThisMethod('value')

I hope that all makes sense.

Re: Why $this-> with properties???

Posted: Tue Oct 12, 2010 8:38 pm
by Jonah Bron
$this is a reference to the current object instance. "->" is an object notation, pointing to a property or method in that object. So, $this->someProperty points to $someProperty in the current object. Note, $this is only used in code that's inside of an object. Here's an example.

Code: Select all

class TestClass {
    public $someProperty = "hello";
    public function someMethod() {
        echo $this->someProperty;
    }
}

$a = new TestClass();
$b = new TestClass();

echo $a->someProperty; //outputs "hello"
$a->someMethod(); // outputs "hello"

echo $b->someProperty; //outputs "hello"
$b->someProperty = "Bob";
echo $b->someProperty; //outputs "Bob"
echo $a->someProperty; //outputs "hello"
$b->someMethod(); //outputs "Bob"
$a->someMethod(); //outputs "hello"
So you see, someProperty is a property of the class TestClass. But, each instance of that class has it's own someProperty. In the example, we changed $b->someProperty, but $a->someProperty remained the same. Even though it's the same class, it's a different object/"class instance".

You also see how we could access someProperty from inside the class, with the $this special variable.

I hope I answered you question :)

Re: Why $this-> with properties???

Posted: Tue Oct 12, 2010 8:42 pm
by ATLChris
Jonah Bron wrote:$this is a reference to the current object instance. "->" is an object notation, pointing to a property or method in that object. So, $this->someProperty points to $someProperty in the current object. Note, $this is only used in code that's inside of an object. Here's an example.

Code: Select all

class TestClass {
    public $someProperty = "hello";
    public function someMethod() {
        echo $this->someProperty;
    }
}

$a = new TestClass();
$b = new TestClass();

echo $a->someProperty; //outputs "hello"
$a->someMethod(); // outputs "hello"

echo $b->someProperty; //outputs "hello"
$b->someProperty = "Bob";
echo $b->someProperty; //outputs "Bob"
echo $a->someProperty; //outputs "hello"
$b->someMethod(); //outputs "Bob"
$a->someMethod(); //outputs "hello"
So you see, someProperty is a property of the class TestClass. But, each instance of that class has it's own someProperty. In the example, we changed $b->someProperty, but $a->someProperty remained the same. Even though it's the same class, it's a different object/"class instance".

You also see how we could access someProperty from inside the class, with the $this special variable.

I hope I answered you question :)
That defiantly helps. OOP is just so confusing to me. Thank you!