PHP中self :: $ bar和static :: $ bar有什么不同?
可能重复:
新的自我与新的静态
在下面的例子中,使用self和static什么区别? 
 class Foo { protected static $bar = 1234; public static function instance() { echo self::$bar; echo "\n"; echo static::$bar; } } Foo::instance(); 
产生
 1234 1234 
	
 当您使用self来引用类成员时,您指的是使用关键字的类。 在这种情况下,您的Foo类定义了一个名为$bar的受保护静态属性。 当您在Foo类中使用self来引用属性时,您引用的是同一个类。 
 因此,如果您尝试在Foo类的其他地方使用self::$bar ,但是您的Bar类具有不同的属性值,则将使用Foo::$bar而不是Bar::$bar ,而这可能不是你打算如何 
 class Foo { protected static $bar = 1234; } class Bar extends Foo { protected static $bar = 4321; } 
 当你使用static ,你正在调用一个叫做late static bindings (在PHP 5.3中引入)的特性。 
 在上面的场景中,使用static而不是self会导致使用Bar::$bar而不是Foo::$bar ,因为解释器会考虑Bar类中的重新声明。 
 你通常使用晚期的静态绑定方法,甚至是类本身,而不是属性,因为你不经常重新声明子类中的属性; 在这个相关的问题中可以find一个使用static关键字来调用late-bound构造函数的例子: New self vs. new static 
 但是,这并不排除使用static属性。 
 self:- refers to the same class whose method the new operation takes place in. static:- in PHP 5.3's late static binding refers to whatever class in the hierarchy which you call the method on. 
在下面的例子中,Binheritance了A的两个方法,self被绑定到A,因为它在A的第一个方法的实现中被定义,而static被绑定到了被调用的类(也参见get_called_class())。
 class A { public static function get_A() { return new self(); } public static function get_me() { return new static(); } } class B extends A {} echo get_class(B::get_A()); // A echo get_class(B::get_me()); // B echo get_class(A::get_me()); // A