Don't do this: nonexistent trait fields usage

With this article, I open a rubric "Don't do this." It will contain code from real projects, from which I will remove all references to the projects themselves. And, of course, my explanations of why shouldn't you do this, and the way it should be done.

So, the first patient. Inside the trait, the fields of its descendants are used. I mean something like this:

trait Foo
{
    public function bar(): string
    {
        return $this->field . static::CONSTANT . $this->method();
    }
}

Note

Or even worse, $this->{$attribute}. But it's out of the question, as the chance of getting an error 500 rises to almost 100%.


April 23, 2023
About 2 min