Page 1 of 1

file naming conventions for traits / interfaces

Posted: Mon Sep 14, 2015 10:08 am
by gerijennings
I'm trying out using traits & interfaces for the first time, and trying to figure out best practices for including them. We do not use a framework.

We keep our PHP classes in their own directory with filenames like "class.[classname].php". We then define an autoloader that loads files in classDirectory/class.{classname}.php in each PHP file that requires a class.

What should I name the file containing my trait or interface? It feels like "class.[classname].php" isn't appropriate, because they aren't classes, but if I name it differently, I have to modify all of my autoloads. Thoughts?

Re: file naming conventions for traits / interfaces

Posted: Mon Sep 14, 2015 10:18 am
by Celauran
What is the setup of the application? Sounds like there's lots of duplication going on, which isn't ideal. If at all possible, I'd look at implementing namespacing and a PSR-4 autoloader. If you can't do that, implementing a second autoloader (only once, if at all possible) that loads trait.{traitname}.php is an option.

Re: file naming conventions for traits / interfaces

Posted: Mon Sep 14, 2015 10:31 am
by gerijennings
Yes, I was kind of getting the sense that namespaces might be starting to make sense. I'm making this particular mod as a contractor for a company, so I don't have the ability to undertake such a big project right now. I wonder if a good middle ground is to create a new traits/ directory that has trait.[traitname].php in it, and use vim to update all the autoloaders to also autoload everything in the traits directory. I get that this is not ideal because the autoloading will still be repeated in the PHP files, but maybe this will start getting this client on the right path?

Re: file naming conventions for traits / interfaces

Posted: Mon Sep 14, 2015 10:39 am
by Celauran
Even small improvements are worthwhile. You could go with the traits directory, though if you're prefixing class files with class. then prefixing traits with trait. in the same directory could also make sense.