du248227 2014-11-20 11:47
浏览 68
已采纳

在Composer中为导入的第三方包添加名称空间

I am importing a third-party package into my project using composer.

The composer.json of the package autoloads its classes using "classmap":

{
  ...
  "name"=>"vendor/project",
  ...
  "require": {
    "php": ">=5.2.0"
  },
  "type": "library",
  "include-path":["src/"],
  "classmap": [
    "src/path/to/lib1",
    "src/path/to/lib2"
  ]
  ...
}

My project composer.json pulls the package in using "require".

{
  ...
  "require": {
    "vendor/project": "m.n.*",
  }
  ...
}

I'd like to add a namespace that can prefix all the classes of that package when I use it in my project, can I do this in composer?

I am aware I can use autoload at the level of my project, but presumably these classes don't need loading again and where do I point it?

  • 写回答

1条回答 默认 最新

  • dongyutan1703 2014-11-20 11:56
    关注

    You cannot add a namespace to a project without editing every individual file in the project and adding a namespace .. declaration at its top. This is likely infeasible.

    If you namespace your own code, there should be no possible problem of a name clash.
    If the library clashes with yet another non-namespaced third party library which you also cannot feasibly namespace, then you're in trouble. Unless this is the case, there's no real reason to worry about it.

    If composer's definition is set up properly, all you should need to do is simply use the class:

    $foo = new \VendorClass;
    

    Composer's autoloading will take care of loading the class, a missing namespace is of no concern (see above).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失