static:: 和 $this:: 之间的区别 [英] Difference between static:: and $this::
问题描述
我知道 static::
和 self::
之间有区别,就像这个例子(来自 https://stackoverflow.com/a/13613718/2342518 )
test();
当使用 static::TEST
时返回 test2,使用 self::TEST
时返回 test1.但是当使用 $this::TEST
时,它也会返回 test2.
static::TEST
可以在静态方法中使用,而 $this::TEST
在使用之前需要一个实例(因此在静态方法).
但是如果不能在静态方法中使用 $this::
,static::
可以在非静态方法中使用(如示例中所示).>
那么,在非静态方法中,static:: 和 $this:: 有什么区别?
<小时>可选的完整测试
新的 OneStatic(),'二,静态::' =>新的 TwoStatic(),'一,自我::' =>新的自己(),'二,自我::' =>新的 TwoSelf(),'一,$this::' =>新的 OneThis(),'二,$this::' =>新的 TwoThis(),);$results = array();foreach ($objects as $name=>$object)$results[$name] = $object->test();var_dump($results);?>
哪个收益
- 'one, static::' => 'test1'
- '二,静态::' => 'test2'
- 'one, self::' => 'test1'
- '二,自我::' => 'test1'
- 'one, $this::' => 'test1'
- '二,$this::' => 'test2'
so self 指的是定义方法的类,但是在这些非静态方法中 $this::
和 static::
没有区别.>
不能使用 $this::
而不是 static 的三种情况::
1.在静态方法中
public 静态函数 test() {返回 $this::MY_CONST;}
输出:
<块引用>致命错误:未捕获错误:不在对象上下文中使用 $this
2.在非对象上下文中调用的非静态方法
class A {const MY_CONST = 33;公共功能测试(){返回 $this::MY_CONST;}}回声 A::test();//调用这里的测试方法,没有实例化A类
输出:
<块引用>致命错误:未捕获错误:不在对象上下文中使用 $this
3.当使用特殊的 ::class
关键字
class A {公共功能测试(){返回 $this::class;}}$a = 新 A;echo $a->test();
输出:
<块引用>致命错误:编译时不允许使用动态类名
注意:在所有三种情况下static::
都可以使用
对于最后一种情况PHP 文档声明:
<块引用>注意:
使用::class的类名解析是编译时转型.这意味着当时类名字符串是created 尚未发生自动加载.因此,类名即使该类不存在,也会被扩展.没有错误发出那种情况.
所以你不能使用 $this::class
因为你不能引用不存在的类
PHP 8
PHP 8 中的行为已经改变!
出于一致性原因,$this::class
现在提供与 get_class($this)
和 static::class
相同的结果>
I know there is a difference between static::
and self::
like in this example ( from https://stackoverflow.com/a/13613718/2342518 )
<?php
class One
{
const TEST = "test1";
function test() { echo static::TEST; }
}
class Two extends One
{
const TEST = "test2";
}
$c = new Two();
$c->test();
Which returns test2 when static::TEST
is used and test1 when self::TEST
is used.
But it also returns test2 when $this::TEST
is used.
static::TEST
can be used inside a static method, whereas $this::TEST
requires an instance before being used (so non-usable in static methods).
But if one cannot use $this::
in static methods, static::
can be used in non-static methods (like in the example).
So, what is the difference between static:: and $this:: in a non static method?
Optional complete test
<?php
abstract class AOne
{
const TEST = "test1";
abstract public function test();
}
class OneStatic extends AOne
{
public function test()
{
return static::TEST;
}
}
class TwoStatic extends OneStatic
{
const TEST = "test2";
}
class OneSelf extends AOne
{
public function test()
{
return self::TEST;
}
}
class TwoSelf extends OneSelf
{
const TEST = "test2";
}
class OneThis extends AOne
{
public function test()
{
return $this::TEST;
}
}
class TwoThis extends OneThis
{
const TEST = "test2";
}
$objects = array(
'one, static::' => new OneStatic(),
'two, static::' => new TwoStatic(),
'one, self::' => new OneSelf(),
'two, self::' => new TwoSelf(),
'one, $this::' => new OneThis(),
'two, $this::' => new TwoThis(),
);
$results = array();
foreach ($objects as $name=>$object)
$results[$name] = $object->test();
var_dump($results);
?>
Which yields
- 'one, static::' => 'test1'
- 'two, static::' => 'test2'
- 'one, self::' => 'test1'
- 'two, self::' => 'test1'
- 'one, $this::' => 'test1'
- 'two, $this::' => 'test2'
So self refers to the class where the method is defined, but there's no difference between $this::
and static::
in these non static methods.
There are three cases when you CAN'T use $this::
over static::
1. In static methods
public static function test() {
return $this::MY_CONST;
}
Output:
Fatal error: Uncaught Error: Using $this when not in object context
2. In none-static methods which get called in none-object context
class A {
const MY_CONST = 33;
public function test() {
return $this::MY_CONST;
}
}
echo A::test(); // test method here is called without instantiating class A
Output:
Fatal error: Uncaught Error: Using $this when not in object context
3. When using the special ::class
keyword
class A {
public function test() {
return $this::class;
}
}
$a = new A;
echo $a->test();
Output:
Fatal error: Dynamic class names are not allowed in compile-time
Note: in all the three cases static::
will work
For the last case PHP Documentation states that:
Note:
The class name resolution using ::class is a compile time transformation. That means at the time the class name string is created no autoloading has happened yet. As a consequence, class names are expanded even if the class does not exist. No error is issued in that case.
So you can't use $this::class
because you can't reference to non-existent classes
PHP 8
The behavior in PHP 8 has changed!
For consistency reasons $this::class
now provides the same result as get_class($this)
and static::class
这篇关于static:: 和 $this:: 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!