duanpiangeng8958 2017-01-20 11:17
浏览 51
已采纳

未知项目级别的AutoLoader

I would like to make my projects so fexible that one can simply copy+paste them into each other.

My project structure is ProjectName/(bin, config, lib).

After copy+paste I would have a chain like ProjectName1/lib/ProjectName2/lib/ProjectName3/.

Now it would be great if the Autoloader searches files acording to the level where the file is been called.

For example if a class within ProjectName1/lib/ProjectName2/lib/ calls "new Config" it should receive the config file located in ProjectName1/lib/ProjectName2/config/config.php.

(But not the one in ProjectName1/config/config.php and neither the on in ProjectName1/lib/ProjectName2/lib/ProjectName3/config/config.php).

Is there a way to do this?

Edit: Does it make sense to make files unique? For example: If 'config.php' was 'ProjectName2Config.php' there is (almost) no chance for conflicts. So the autoloader could search everywhere and will eventually find it's file.

Edit: Each project would have his own autoloader availible for copy+paste reasons. However I thought that I would load just the one for ProjectName1. Is it better to load them all so that each one can stay simple?

Cheers, Peter

PS: I just came back from a 3 years programming break. I am also happy, if you tell me that above is gennerally a bad idea and give me the reason why :-)

  • 写回答

1条回答 默认 最新

  • duankezong4064 2017-01-20 11:50
    关注

    I think this is a strange way to manage your projects. Where would be the main script of Project1 and the one of Project2 ?

    For example, what I do is I write this in my index.php or main project/lib include file :

    <?php define('ROOT_PATH', dirname(__FILE__) . '/'); ?>
    

    And I always use this ROOT_PATH as a start of inclusion paths.

    In your case Project1 ROOT_PATH would be something like "/var/www/Project1/" and Project2 ROOT_PATH would be "/var/www/Project1/lib/Project2/"

    Thus a new Config in Project1 and in Project2 would use their own config.php file.

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

报告相同问题?

悬赏问题

  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频