Home > Backend Development > PHP Tutorial > PHP类继承,相关构造函数

PHP类继承,相关构造函数

WBOY
Release: 2016-06-13 11:50:05
Original
951 people have browsed it

PHP类继承,有关构造函数

header("Content-Type:text/html;charset=utf-8");<br /><br />class Class1 extends Class2{<br />    public static $a = 'Class1';<br />}<br />class Class2 extends Class3{<br />    public static $a = 'Class2';<br />    public function __construct(){<br />        echo 'Class2';<br />    }<br />}<br />class Class3{<br />    public static $a = 'Class3';<br />    public function __construct(){<br />        echo 'Class3';<br />    }<br />}<br /><br />$obj = new Class1();<br />
Copy after login


为什么这段代码出错

这里面构造函数是运行两个还是一个
------解决方案--------------------
晕。顺序错了。

header("Content-Type:text/html;charset=utf-8");

class Class3 {

    public static $a = 'Class3';

    public function __construct() {
        echo 'Class3';
    }

}

class Class2 extends Class3 {

    public static $a = 'Class2';

    public function __construct() {
        echo 'Class2';
    }

}

class Class1 extends Class2 {

    public static $a = 'Class1';

}

$obj = new Class1();

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