dongzhuo1930 2015-12-08 12:43
浏览 83
已采纳

多个包含相同类的模块

So I need to include modular code/libraries in my project, a new library:

Library A:

a.php: class A;

Library B: (with v1.0 checked out from A)

b.php: include_once a.php; class B extends A;

Library C: (with v2.0 checked out from A)

c.php: include_once a.php; class C extends A;

Library D:

d.php: include_once b.php,c.php;

This obviously leads to name conflict (cannot redeclare A).

I cannot touch either of the libraries as they are frequently pulled from elsewhere. Also, I need the result to be another library, so nested namespaces is not an option. But I guess there must be plenty of other projects with the same problem?? Thanks for any suggestions.

  • 写回答

3条回答 默认 最新

  • duansan9435 2015-12-08 13:21
    关注

    You can use require_once or include_once, in place of your includes.

    These may affect performance, as memory and processing has to be used to keep track of included classes and check against it, but it's the easiest solution. How much they do, depends on how many include/require_once are in your single page load.

    More complicated ones would implement a class autoloader, but you might not want to go there...

    EDIT--

    If I understand well your problem, it's a dependency problem. Libraries B and C both refer to A, but each refers to a different version of A. If that's the case, the solution is not trivial: any use of require_once or include_once will lead to undetermined first loaded version of A to be used. Any use of include will result in class duplication. I don't think you can solve the issue by just using these functions. What you'd need to do would be to manage library dependencies.

    For example, an autoloader that recognises several versions of a class library and loads the most recent one (this assuming that you keep backwards compatibility across versions). You could also try to use nested classes, to keep each library's dependencies isolated and self-contained.

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

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂