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).

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

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题