PHP类中static 和self的使用区别
By admin
- One minute read - 132 words摘自: http://php.net/manual/en/language.oop5.late-static-bindings.php
Limitations of self::
Static references to the current class like self:: or CLASS are resolved using the class in which the function belongs, as in where it was defined:
Example #1 self:: usage
<code><?php<br /> class A {<br /> public static function who() {<br /> echo __CLASS__;<br /> }<br /> public static function test() {<br /> self::who();<br /> }<br /> }
class B extends A {
public static function who() {
echo CLASS;
}
}
B::test();
?>
The above example will output:
A
===================
class A {
public static function who() {
echo CLASS;
}
public static function test() {
self::who();
// static::who();
}
}
A::test();
class B extends A {
public static function who() {
echo CLASS;
}
}
echo B::test();
如果使用关键字self运行结果: A A
如果使用关键字static运行结果:A B
static:父类访问了子类的静态方法
self: 是类内指针,指向本类,静态方法,属性