Home >Backend Development >PHP Tutorial >基础题:关于parent子类属性?该怎么解决

基础题:关于parent子类属性?该怎么解决

WBOY
WBOYOriginal
2016-06-13 11:49:03924browse

基础题::关于parent子类属性?

class a{<br /><br />   public $name="a";<br />   <br />   public function test1()<br />   {<br />	   echo $this->name;<br />   }<br />}<br /><br />class b extends a<br />{<br /><br />  public $name="b";<br />  <br />  public function test2()<br />  {<br />	 parent::test1();<br />  }<br />}<br /><br />$a=new b();<br />$a->test2();


按理解,调用父类中的 parent::test1(); 应该打印echo "a" ,但是结果打印的却是 echo "b",不理解?
调用父类中的方法怎么会去子类中找属性?

*如果把父类中 $name 改为private 则结果就是echo "a"

请高手指点下啊,谢谢

------解决方案--------------------
print_r($a);

b Object
(
    [name] => b
)

name 属性被覆盖了

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn