I want to use a custom PHP class in my theme, however I am not sure where to add it.

Is there best practice used by the community for including PHP classes with a theme or is that an individual developer’s decision?

The first class I want to add is a custom menu Walker, however there will be more, and they will be tied to my theme.

I am split between creating them in separate PHP files versus adding them to functions.php – or maybe there is any other ways please?

Many thanks in advance for your input.

Katrina

1 Answer
1

Ultimately it’s about maintainability and readability. Functions.php is good for adding just that – functions. Small snippets of code that modify or add to the theme. When you start getting into classes or building some kind of framework, you need to start separating your concerns whether that be moving them into a plugin or simply including your class files into the theme and keeping a well organized directory.

And there’s no performance to contend here – it’s all about what makes development the easiest and most maintainable for you.

Leave a Reply

Your email address will not be published. Required fields are marked *