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 调用的是最高级的父类的变量

猜你喜欢

转载自www.cnblogs.com/pandaLIU/p/12460671.html
今日推荐