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

WBOY
Release: 2016-06-13 11:49:03
Original
899 people have browsed it

基础题::关于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();
Copy after login


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

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

请高手指点下啊,谢谢

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

b Object
(
    [name] => b
)

name 属性被覆盖了

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!