Reputation: 6599
When calling a method in the same class from within another method, is it better to use $this
or to avoid it, or is there no difference at all?
One benefit I can see with using $this
is that it is explicit. For example:
class A {
public function a() {
$x = $this->b();// or $x = b()
}
public function b() {
//
}
}
Upvotes: 0
Views: 61
Reputation: 14649
Unlike other languages, like C++
and C#
and Java
, to access a member property of a class in PHP
, you must always use $this
as a qualifier upon the property.
For example:
class Test {
public $myVariable;
public function __construct($a) {
$this->myVariable = $a;
// $myVariable doesn't exist, must always use $this-><*>
}
}
Upvotes: 1