There is an existing deprecation notice for PHP8.1 which indicates:
Calling a static method, or accessing a static property directly on a trait is deprecated. Static methods and properties should only be accessed on a class using the trait.
The example given in the associated RFC shows accessing a static member from the trait directly, from outside the trait/class. However, it is ambiguous as to whether this applies to self/static contexts inside of traits. What I'm interested in is whether this deprecation applies to accessing static class members from the trait internally, for example:
trait SupportsArrayBackedValues
{
abstract public static function cases();
public function backedValueArray(): array
{
return array_map(fn($case) => $case->value, static::cases());
}
}
PhpStorm 2021.3.1 displays a deprecation warning as such when using static::cases():
Calling static trait member directly is deprecated. It should only be accessed on a class using the trait.
But I'm unsure if this is legitimate, or a bug in how they're parsing the inspection. The inspection offers to convert to self::cases(), which continues to show a deprecation warning. I can find no issues being tracked in IntelliJ's YouTrack for this issue which indicate it's a bug, however.
If this is indeed a valid inspection notice, is there an appropriate way of implementing the functionality I'm seeking, or more generally, accessing static members of a class from inside a trait? (i.e. in my scenario, converting a backed enumeration to an array of string/integer values, rather than enumeration instances).
That sounds like a bug in PHP Storm..
using static::cases() is late static binding, which in PHPStorm resolves to the trait. That is until you use it in a class where it now resolves to the class instead. Silly, if I'm is correct, because the trait can never be used alone anyway so...
These examples illustrate what the deprecation notice was referring to:
Accessing static members on traits PHP8.0
Accessing static members on traits PHP8.1
EDIT: Summarized version
<?php
trait StaticTraitAccess {
public static function staticMethod() {
return 'threturnVal';
}
public function staticCallInTrait() {
var_dump(static::staticMethod());
// the following is deprecated in php8.1
var_dump(StaticTraitAccess::staticMethod());// <--
// ^^^^^^^^^^^^^^^^^
}
}
class StaticClassAccess {
use StaticTraitAccess;
public function staticCalls(): array {
var_dump(static::staticMethod());
var_dump(StaticClassAccess::staticMethod());
// the following is deprecated in php8.1
var_dump(StaticTraitAccess::staticMethod());// <--
// ^^^^^^^^^^^^^^^^^
}
}
$sca = new StaticClassAccess();
$sca->staticCalls();
$sca->staticCallInTrait();
What I'm interested in is whether this deprecation applies to accessing static class members from the trait internally
yes but not accessing statically. difference:
MyTrait::staticMember()static::staticMember()maybe I'm right, maybe I'm crazy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With