今天讲的是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

相关文章:

  • 2021-11-10
  • 2021-11-09
  • 2021-09-30
  • 2021-10-06
  • 2021-08-31
  • 2021-11-15
  • 2021-06-26
  • 2021-08-01
猜你喜欢
  • 2021-12-11
  • 2021-10-23
  • 2022-01-14
  • 2021-11-30
  • 2021-11-12
  • 2021-12-04
  • 2021-09-20
相关资源
相似解决方案