php 中 self 和 static 的区别
2021-04-08 11:27
标签:cti 的区别 class public elf his 区别 function this 使用 使用 php 中 self 和 static 的区别 标签:cti 的区别 class public elf his 区别 function this 原文地址:https://www.cnblogs.com/pandaLIU/p/12460671.htmlphp 中 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
调用的是最高级的父类的变量