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

PHP类继承,有关构造函数

WBOY
Release: 2016-06-23 14:01:20
Original
780 people have browsed it

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();
Copy after login


为什么这段代码出错

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


回复讨论(解决方案)

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中函数预先定义,不问顺序

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