dtd14883 2019-08-13 12:59
浏览 148

如何动态加载类列表?

I would like to load a list of classes dynamically. The general idea would be to have a sort of manifest/config file that contains an array list of classes and then they would "automatically" be loaded into my application instead of needing to manually do something like

use App\Controllers\UserController;
use App\Controllers\ContractController;
use App\Controllers\SubscriptionController;
etc...

It seems there must be some way to achieve this as the Laravel framework does something similar with service providers. You register the classes once in config/app.php and then these provider classes are auto loaded onto the app.

  • 写回答

1条回答 默认 最新

  • dongyong2906 2019-08-13 13:34
    关注

    If you are looking for a native/homemade solution without any PSR0/4 convention or composer package manager, you can consider use spl_autoload_register and __autoload to make your own logic to resolve the classes.

    But i would strongly recommend composer and PSR4 standarts for this.

    Here is the example of a customer resolver/loader class https://www.php.net/manual/en/function.spl-autoload-register.php#102180

    评论

报告相同问题?

悬赏问题

  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答