
illustrate
1. $this is a reference to the current object. There is a pointer in $this. Whoever calls it will point to it. It can only be used within the class.
2. $this cannot be used to access static properties, because static properties are bound to classes.
Can only be accessed by static, self and parent.
Example
class MyClass1
{
public $public = 'Public';
protected $protected = 'Protected';
private $private = 'Private';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
class MyClass2 extends MyClass1
{
public $public = 'Public2';
protected $protected = 'Protected2';
private $private = 'Private2';
}
$obj = new MyClass1();
$obj -> printHello(); //Public Protected Private
$obj2 = new MyClass2();
$obj2 -> printHello(); //Public2 Protected2 PrivateThe above is the introduction to $this in php. I hope it will be helpful to everyone.