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 Drupal\my_module;

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

use Drupal\my_module\CustomClass;

我的个人猜想是:

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

posted @ 2018-03-01 10:46  qqisnow2021  阅读(123)  评论(0)    收藏  举报