dragon201401 2018-11-23 15:58
浏览 115

如何使用命名空间中的所有类而不是单独列出它们?

In my Controller I list all entities I have:

use App\Entity\Members;
use App\Entity\Products;
use App\Entity\Projects;

But because new Entities will automatically be created, I need instead something like

use AllEntities;

Is this possible?

  • 写回答

1条回答 默认 最新

  • dongxun1244 2018-11-23 16:15
    关注

    There is no available option in PHP to import all classes from particular namespace at once.

    In order to use a particular class you need to define so called Fully-qualified class name so it means to use use keyword for each class OR the other option is to import the namespace but when instantiating or using that class as a static for example you need to use imported namespace followed by \ and the class name.

    For example: use Foo\Bar; $classOne = new Bar\ClassOne(); $classTwo = new Bar\ClassTwo();

    This is valid when there is only one namespace ending with Bar imported. Otherwise you need to alias each one - the same applies to repeated class names - they also need to be aliased. This is about the built-in possibilities.

    When it comes to your particular case - if you automatically create new entities which means that you generate new code - the only solution I see here is to expand your generation capabilities for generating some new code in places you need it.

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作