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.
Why $this-> with properties???
Moderator: General Moderators
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Why $this-> with properties???
$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.
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
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"
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???
That defiantly helps. OOP is just so confusing to me. Thank you!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.
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".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"
You also see how we could access someProperty from inside the class, with the $this special variable.
I hope I answered you question