file naming conventions for traits / interfaces

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gerijennings
Forum Newbie
Posts: 2
Joined: Sat Sep 12, 2015 1:13 pm

file naming conventions for traits / interfaces

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: file naming conventions for traits / interfaces

Post 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.
gerijennings
Forum Newbie
Posts: 2
Joined: Sat Sep 12, 2015 1:13 pm

Re: file naming conventions for traits / interfaces

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: file naming conventions for traits / interfaces

Post 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.
Post Reply