日期:2014-05-17  浏览次数:20396 次

关于php继承的疑惑
PHP code

<?php
class A {
    [color=#FF0000]private[/color] $name = "Nciaer";

    public function say() {
        echo $this -> name . "<br/>";
    }
}

class B extends A {
    public $name = "Fansa";

}

$b = new B();
$b -> say();
?>


输出结果:
Nciaer
----------------------------

PHP code

<?php
class A {
    [color=#FF0000]public[/color] $name = "Nciaer";

    public function say() {
        echo $this -> name . "<br/>";
    }
}

class B extends A {
    public $name = "Fansa";

}

$b = new B();
$b -> say();
?>



输出结果:
Fansa


为什么父类变量$name为public的时候,$b调用成员方法say()输出子类的$name;
当父类变量$name为private时,$b调用成员方法say()输出父类的$name;

------解决方案--------------------
private 私有的
怎么能被继承呢?