php 中 self 和 static 的区别

php 中 self 和 static 的区别


class Foo
{
    public static $str = 'This is foo';

    public static function show()
    {
        echo __METHOD__ . PHP_EOL;
        echo static::$str;
    }
}

class Boo extends Foo
{
    public static $str = 'This is boo';
}

Boo::show();

# 输出结果
# Foo::show
# This is boo

使用 static 调用的是当前类的变量

class Foo
{
    public static $str = 'This is foo';

    public static function show()
    {
        echo __METHOD__ . PHP_EOL;
        echo self::$str;
    }
}

class Boo extends Foo
{
    public static $str = 'This is boo';
}

Boo::show();

# 输出结果
# Foo::show
# This is foo

使用 self 调用的是本身所在的那个类的变量

posted @ 2020-03-11 10:01  pandaLIU  阅读(297)  评论(0)    收藏  举报