PHP类中static 和self的使用区别

来源:互联网 发布:淘宝网皮衣加厚的 编辑:程序博客网 时间:2024/06/07 16:29
摘自: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

<?php
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
self::who();
}
}
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: 是类内指针,指向本类,静态方法,属性

原创粉丝点击