<?php
class A{
public $a='bb';
public function getA(){
return 'BB';
}
public function parse(){
echo 'ttt'.$this->a."\n";
}
}
class B extends A {
public $a='CC';
public function getA(){
return 'DD';
}
public function parse(){
parent::parse(); //此时的this为父类中的parse()方法 中的$this为当前B类中的对象而不再是A类中的。
echo $this->getA();
}
}
$s = new B;
$s->parse();
/*
output:
tttCC
DD
class A{
public $a='bb';
public function getA(){
return 'BB';
}
public function parse(){
echo 'ttt'.$this->a."\n";
}
}
class B extends A {
public $a='CC';
public function getA(){
return 'DD';
}
public function parse(){
parent::parse(); //此时的this为父类中的parse()方法 中的$this为当前B类中的对象而不再是A类中的。
echo $this->getA();
}
}
$s = new B;
$s->parse();
/*
output:
tttCC
DD
*/
结论:关于在子类中使用parent关键字时$this在父类中的归属,他是属于当前子类,尽管这个$this是写在父类当中。