Home > Backend Development > PHP Tutorial > 写了段代码,不知道该怎么理解面向对象的继承

写了段代码,不知道该怎么理解面向对象的继承

WBOY
Release: 2016-06-13 12:35:26
Original
830 people have browsed it

写了段代码,不知道该如何理解面向对象的继承

本帖最后由 chaoxi1991 于 2013-10-18 16:36:27 编辑
class ParentClass {<br />
    private $private = 1;<br />
    public function getPrivate()<br />
    {<br />
        echo 'getPrivate() belong to class "' . get_class($this) . '"<br>';<br />
        return $this->private;<br />
    }<br />
}<br />
<br />
class Son extends ParentClass {<br />
}<br />
<br />
$son = new Son();<br />
echo 'private=' . $son->getPrivate();
Copy after login

我预期结果是应该报错的,但并没有报错。
执行结果是:
in class "ParentClass" function getPrivate():"Son"
private=1
想问为什么$private私有属性会被打印出来呢
PHP 面向对象 继承
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