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,但它指的是被调用的类而不是包含类