A neat feature of included files is the fact that they can return data. You can simply assign the output of an include
or 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.