php static延迟静态绑定

一直都不知道php还有这样的功能呢,见识了。

 1 <?php
 2 /**
 3  * Page48 abstract interface static
 4  * @authors haidong (admin@zhe700.net)
 5  * @date    2015-03-25 13:39:29
 6  * @version $Id$
 7  */
 8 
 9 abstract class DomainObject{
10     public static function create(){
11         //return new self();//用self会出错Cannot instantiate abstract class DomainObject
12         return new static();//static延迟静态绑定
13     }
14 }
15 class User extends DomainObject{
16 }
17 class Document extends DomainObject{
18 }
19 print_r(Document::create());

在用self时就会创建实例,而抽象类又不能创建实例,所以就会报错Cannot instantiate abstract class DomainObject,这个时候使用static恰恰就可以很好解决这个问题。

posted @ 2015-03-25 14:04  我的代码  阅读(119)  评论(0编辑  收藏  举报