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

基础题:关于parent子类属性?解决方案

WBOY
Release: 2016-06-13 12:30:11
Original
1005 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"

请高手指点下啊,谢谢

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