Alternative for .inc files I was wondering what is the correct alternative of .inc files in Drupal 8? That is to say, if I want to create some functions that should be only be loaded when required, like it was with module_load_include() in D7. Though this function is still there in D8, I hear that this is not the best procedure. Is that so?

I have read somewhere, that using PHP traits is considered a better practice here. However, not sure if useing a trait inside a .module file is a good approach.

Any help?
Actually there seems to be nothing wrong about using *.inc files in D8. Have a look at the Update core module. Quite some *.inc files present: cgit.drupalcode.org.



Nevertheless I tend to follow Symfony's best practice examples when it comes to stuff like that.

[Symfony Docs] Organizing Your Business Logic

[Stack Overflow] Where to put model helper functions in Symfony2

Means you'll add a new class under src/Utils. For example:

src/Utils/HelperFunctions.php
src/Utils/FooBar.php


And then wherever you need it, you reference it:

use Drupal\MYMODULE\Utils\HelperFunction;
use Drupal\MYMODULE\Utils\FooBar;


And then you'd call a certain function like so:

$site_color = HelperFunctions::getSiteColor();
$foo_bar    = FooBar::returnFooBar();




And when I'm still unsure, I'll search inside the core and contrib modules how they did it.