Best way to share custom PHP code amongst projects

霸气de小男生 提交于 2019-12-12 10:03:18

问题


I'm developing a distributed environment, mostly in PHP. A lot of the projects that I have share some of the same code. For example my logging code (based on log4php but with some custom additions).

I can just copy-paste this code in every project, but naturally, if I change anything in it I need to re-paste it everywhere and 'hope' it doesn't change anything. Obviously not a good way.

Now, I was wondering: what is the best and simplest way of sharing this code? I'm hoping there is something as easy as making your own private PEAR-like channel? Do people do this for this use case? Or is there an ever easier way? I rather not start with inter-dependent repositories etc. If it matters though, I use Mercurial for versioning.

Thanks all.


回答1:


Perhaps the best way (I am using too) is to host the common code in a versioning system such as Git or Mercurial or SVN and checkout this code in each related project. As long such a project does not have updates you can continue to work on your common code. In the case you want to touch a project again that utilizes the common code, you need to perform a source code update or pull. Then you update your project sources according to the changes of the common source base.




回答2:


What about include_path PHP directive?

Just add include_path, for example /usr/share/php5/

and include your file like yourfile.php from each project




回答3:


Use Git or Mercurial for that. In both of them, you can create sub-modules or sub-repositories.

http://kernel.org/pub/software/scm/git/docs/git-submodule.html

https://www.mercurial-scm.org/wiki/Subrepository



来源:https://stackoverflow.com/questions/6891071/best-way-to-share-custom-php-code-amongst-projects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!