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 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要