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

PHP类继承,有关构造函数

WBOY
WBOYOriginal
2016-06-23 14:01:20780browse

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


为什么这段代码出错

这里面构造函数是运行两个还是一个


回复讨论(解决方案)

Class2 构造覆盖了Class3 
Class2 构造里面加入 parent::__construct();
试试

这段代码运行出错  
PHP Fatal error:  Class 'Class2' not found

晕。顺序错了。

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();

 
OK ,good,   以前搞JS的,  js中函数预先定义,不问顺序

Statement:
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