php 延迟静态绑定: static关键字

abstract class DomainObject {
    public static function create()
    {
        return new self();
    }
}

class User extends DomainObject {}

class Document extends DomainObject {}

print_r(Document::create());

以上代码运行失败,因为 self 被解析为 DomainObject,将 self 换为 static

abstract class DomainObject {
    public static function create()
    {
        return new static();
    }
}

class User extends DomainObject {}

class Document extends DomainObject {}

print_r(Document::create());

运行成功,浏览器输出为

Document Object ( )

static 类似于 self,但它指的是被调用的类而不是包含类

posted @ 2013-10-31 11:02  hailπ  阅读(422)  评论(0编辑  收藏  举报