PHP5心得3

 今天讲的是parent::和self::两个新的关键字。self是用来引用当前类并且一般用来访问静态成员变量,parent::用来访问父类,并且一般用来访问父类的构造函数或者方法。有OOP知识的朋友其实很容易理解,以下是例子:
class Ancestor {
    const NAME = "Ancestor";
    function __construct()
    {
        print "In " . self::NAME . " constructor\n";
    }
}

class Child extends Ancestor {
    const NAME = "Child";
    function __construct()
    {
        parent::__construct();
        print "In " . self::NAME . " constructor\n";
    }
}

$obj = new Child();
输出是:
In Ancestor constructor
In Child constructor

你可能感兴趣的:(oop)