class Bar {
//use Foo;
if ( trait_exists('foo') ) { use \Foo; }
public function .......
}
I need to check trait exists before "use" it, but i got error. Is anyone know how to achieve this?
Only define the class if the trait exists:
if (trait_exists('Foo')) {
class Bar {
use \Foo;
}
} else {
// define alternative Bar class or throw error
}
if (trait_exists('Foo')) { $bar = new class { use \Foo; /* ... */ }; }. You probably should restructure your code. Traits are probably not the solution to your issue.