A neat feature of included files is the fact that they can return data. You can simply assign the output of an
require statement to a variable and get to work on whatever is returned from that file.
Personally, I use this for configuration data where I have arrays full of options. I could load this all up in a class but by keeping it in a separate file, I keep the codebase that little bit cleaner and can store many configuration data sets in the one place. This is actually something I picked up whilst working with Laravel a while back and now implement in many of my projects including the plugin boilerplate that I’ve been crafting over the past few months.
Here’s a look at a file returning an array of data:
And here’s the return value from that file being assigned to a variable:
It’s a simple technique but I dig it.
If you liked this article, I'd be incredibly grateful if you tweeted about it.
Also, I don't send emails often but when I do, I try to fill them full of useful goodies. If you like code snippets & dev tips, join my mailing list. There's no catch but I'll probably want to tell you about any new plugins & tools I build.