Assume this situation:
interface IFoo
{
public function Bar();
}
trait Foo
{
public function Bar()
{
echo 'Bar';
}
}
class FooBar implements IFoo
{
use Foo;
}
$foobar = new FooBar();
$foobar->Bar(); //echos 'Bar';
I put class FooBar in its own file, and interface IFoo also in it own file.
But what should I do with trait Foo:
- In its own file? (my preference)
- Together with the inferface IFoo?
- When the trait is given its own file, how will autoloading deal with that?
note: netbeans marks the code of class FooBar as invalid. Netbeans does not detect that trait Foo is used to implement IFoo. Bug?
Traits are autoloaded just like classes. So if you have a PSR compliant loader (where namespaces are directories, and filenames are class/interface/trait names) everything should work with a trait in it’s own file.
So to answer the question in the title, if you wish to stick to standards, then you should use PSR loading, so yes, traits should be in separate files.
As far as netbeans is concerned, I think 7.2 is the only version that supports php 5.4 (and with that, traits), so make sure that you have netbeans 7.2, and that you set php 5.4 in the project settings.