load custom class in drupal 8

I'd like to add that for including a custom class that doesn't fit into the D8 "plugin, controller, form, etc." defaults, you can still do so as noted PSR-4 overview here

For my use case, I dropped a "CustomClass.php" in /modules/custom/my_module/src/ At the beginning of the file I included the namespace declaration

namespace Drupalmy_module;

and in the file I wanted to use it in (in this particular case my_theme.theme) I added

use Drupalmy_moduleCustomClass;

我的个人猜想是:

src/form
src/plugin这些可能是因为annotation系统注册了psr4的namespace autoload
所以才可以自动加载的 不然也是不行的

原文地址:https://www.cnblogs.com/qinqiu/p/8487313.html