website/errors/new.trait.md
<?php declare(strict_types = 1);
trait MyTrait
{
public function doSomething(): void {}
}
$obj = new MyTrait(); // error: Cannot instantiate trait MyTrait.
Traits in PHP are not standalone classes and cannot be instantiated with the new keyword. A trait is a mechanism for code reuse — it provides methods that can be included in one or more classes using the use keyword. Attempting to instantiate a trait will result in a fatal error at runtime.
Use the trait inside a class and instantiate that class instead:
trait MyTrait
{
public function doSomething(): void {}
}
+class MyClass
+{
+ use MyTrait;
+}
+
-$obj = new MyTrait();
+$obj = new MyClass();