Why $this-> with properties???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ATLChris
Forum Newbie
Posts: 15
Joined: Mon Feb 09, 2009 6:21 am
Location: Atlanta, GA

Why $this-> with properties???

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

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

Post 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 :)
User avatar
ATLChris
Forum Newbie
Posts: 15
Joined: Mon Feb 09, 2009 6:21 am
Location: Atlanta, GA

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

Post 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!
Post Reply