I've been trying to understand how static scope works in the context of a trait. A great explanation is here: https://stackoverflow.com/a/56935557/2137316 but it doesn't quite address my concern.
I'm attempting to create a trait for unit tests possessing a property that more or less serves as global flag. The goal is for it to prevent unnecessary reruns of its behavior if a previous test in a given run has already triggered it.
trait CreateDatabase
{
protected static bool $hasRun = false; //We don't want to waste time rebuilding the database in each test
protected runDb(): void
{
if (!self::$hasRun) {
//Do stuff...
}
self::$hasRun = true;
}
}
class SomeTestClass
{
use CreateDatabase;
/**
* @test
**/
public function canRunSomeTest()
{
$this->runDb();
//Test stuff...
}
}
The related post talks about trait context and would seem to suggest that self
in this context refers to the 'context' of SomeTestClass
, meaning SomeOtherTestClass
would have no awareness that the database has already been created. The conclusion would then seem to be that the way to achieve the effect I'm going for would be for is to replace
self::$hasRun = true;
with
CreateDatabase::$hasRun = true;
Even though that line is being executed within the trait explicitly being referenced. The purpose being, to talk to the more-global trait context rather than that of the class using it.
The problem is, Php8, via my IDE, is fussing at me over that decision:
Calling static trait member directly is deprecated. It should only be accessed on a class using the trait.
Usually when my tooling resists me, it means there's something flawed about the overall approach, but I'm not seeing it yet. Wondering if anyone has any insight.