码迷,mamicode.com
首页 > Web开发 > 详细

PHP new self与new static

时间:2015-12-04 18:07:14      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

self refers to the same class whose method the new operation takes place in.

static in PHP 5.3‘s late static bindings refers to whatever class in the hierarchy which you call the method on.

In the following example, B inherits both methods from A. self is bound to A because it‘s defined in A‘s implementation of the first method, whereas static is bound to the called class (also see  get_called_class() ).

class A {
  public static function get_self() {
    return new self();
  }
 
  public static function get_static() {
    return new static();
  }
}
 
class B extends A {}
 
echo get_class(B::get_self()); // A
echo get_class(B::get_static()); // B
echo get_class(A::get_static()); // A

  

PHP new self与new static

标签:

原文地址:http://www.cnblogs.com/flytome/p/5019747.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!