关键字$this代表其所在的当前对象
使用当前对象的属性和方法 $this->取值
方法内的局部变量 不能用$this 关键字取值
- /*
- 和java,c++相比
- 方法体内想访问调用者的属性,必须用$this
- 如果不加,则理解为方法内部的一个局部变量.
- */
要调用全局变量:必须用$this来调用
例子
class A extends AdminbaseController{
protected $id=0;
public function index(){
echo $this->id;//调用得到
echo $id;//调用失败
$this->display();
}
}
要调用局部变量,$id理解为局部变量
class A extends AdminbaseController{
protected $id=0;
public function index(){
$id=0;
echo $id;//调用失败
$this->display();
}
}