Inside an enum or final
/anonymous class, self
should be preferred over
static
.
--- Original
+++ New
<?php
final class Sample
{
private static $A = 1;
public function getBar()
{
- return static::class.static::test().static::$A;
+ return self::class.self::test().self::$A;
}
private static function test()
{
return 'test';
}
}
--- Original
+++ New
<?php
final class Foo
{
public function bar()
{
- return new static();
+ return new self();
}
}
--- Original
+++ New
<?php
final class Foo
{
public function isBar()
{
- return $foo instanceof static;
+ return $foo instanceof self;
}
}
--- Original
+++ New
<?php
$a = new class() {
public function getBar()
{
- return static::class;
+ return self::class;
}
};
--- Original
+++ New
<?php
enum Foo
{
public const A = 123;
public static function bar(): void
{
- echo static::A;
+ echo self::A;
}
}
The rule is part of the following rule set:
- Fixer class: PhpCsFixer\Fixer\ClassNotation\SelfStaticAccessorFixer
- Test class: PhpCsFixer\Tests\Fixer\ClassNotation\SelfStaticAccessorFixerTest
The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise.