Home > Backend Development > PHP Tutorial > 关于php承袭的疑惑

关于php承袭的疑惑

WBOY
Release: 2016-06-13 10:37:27
Original
897 people have browsed it

关于php继承的疑惑

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?phpclass 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();?>
Copy after login

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

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?phpclass 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();?>
Copy after login


输出结果:
Fansa


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

------解决方案--------------------
private 私有的
怎么能被继承呢?
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