website/errors/traitUse.class.md
<?php declare(strict_types = 1);
class Helper
{
public function help(): void
{
}
}
class MyClass
{
use Helper;
}
The use statement inside a class body is attempting to use a class as if it were a trait. The use keyword in a class body is reserved exclusively for traits. Classes cannot be used with the use statement. This code will result in a fatal error at runtime.
If the intent is to reuse functionality from the class, extend it instead:
<?php declare(strict_types = 1);
-class MyClass
+class MyClass extends Helper
{
- use Helper;
}
If inheritance is not appropriate, use composition:
<?php declare(strict_types = 1);
class MyClass
{
- use Helper;
+ private Helper $helper;
+
+ public function __construct()
+ {
+ $this->helper = new Helper();
+ }
}
Or convert Helper into a trait if code reuse via traits is the intended approach:
<?php declare(strict_types = 1);
-class Helper
+trait Helper
{
public function help(): void
{
}
}