doupo5178 2014-08-18 08:43
浏览 71
已采纳

在单个文件中使用类的作曲家自动加载

I'm trying to use a library that uses namespaces but has a part of the code auto-generated so they generate more than one class in a single file.

We use composer and I tried to add the namespace define in psr-4 like this

"name\space\preffix\": "folder/where/the/file/is"

But there's only one file that contains all the classes inside the autoload doesn't find the classes as, I imagine, it searches a file with the same name as the class you are trying to load. Is there a way to make composer autoload aware of this situation and use autoload with the classes ?

  • 写回答

2条回答 默认 最新

  • douji6940 2014-08-18 23:28
    关注

    You have two other options besides PSR-4 (or PSR-0):

    1. classmap - this will scan directories or files for all classes that are contained, and the result is put into a PHP array in a file. This requires to dump the autoloader whenever there is a change being made to the files being scanned.
    2. files - the mentioned file(s) will be included whenever the Composer autoloader is included.

    So you could either add the file with the autogenerated classes to be scanned with the classmap autoloader, which would load that file on the first usage of ANY of the classes in there, or you could add it to the files autoloading, which will always be included, no matter if the classes are being used or not.

    If considering performance, the first alternative is preferred unless the amount of classes is huge and the amount of code in the classes is tiny. Having plenty of tiny classes in a classmap probably is more overhead than always just loading them in the first place.

    Having plenty of code in these classes, and they are not always used, the amount or memory saved by NOT always loading them might be faster.

    If in doubt: Measure it. And consider to split the classes into single files and use PSR-4 if it is too much of a performance penalty.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。