在 PHP 中创建共享库
我正在处理一些基于 php 的项目,并且遇到必须使用通用代码维护单独的代码库的问题。所有项目之间共享的通用代码是物理添加的(文件复制到文件夹中),当需要更改时,我们必须手动复制每个项目中通用代码功能中的修复。
在java中,我们可以创建一个库(jar文件)并在不同的项目之间共享它。 PHP 5.0 中是否有任何与共享库相关的概念?
Im working on a few php based projects and im having an issue of having to maintain separate code bases with common code. The common code shared amongst all the projects is added physically (file are copied in the folder) and when a change is needed we have to manually replicate the fixes in the common code functionality in each project.
In java we can create a library (jar file) and share it among different projects.
Is there any shared library related concept available in PHP 5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
PHP 版本的 jar 称为 Phar:
http://php.net/manual/en/book.phar.php
PHP's version of a jar is called Phar:
http://php.net/manual/en/book.phar.php
我也一直在思考同样的问题。以下是一些想法。
方法
1:将包含路径设置为所有项目中的一个共享文件夹。如果您在编写/修改库时小心向后兼容性,它不应该破坏任何东西。但是,这是非常危险的,因为如果你确实破坏了某些东西,那么你的所有项目都会被破坏。
2:创建一个包含在库中的简单更新脚本,将文件从主源复制到您的项目。更新库时运行此脚本。
3:使用版本控制设置。 Git 子模块可能有效 ( http://book.git-scm.com/5_submodules.html )
4: Phar 文件是我想尝试的。唯一的缺点是它们不适用于低于 5.3 的 PHP 版本
更新:04/12/2011 - 我建议使用方法#3。现在我已经使用它一段时间了,这显然是最佳实践。
I've also been pondering the same problem. Here are some thoughts.
Approaches
1: Set the include path to one shared folder from all your projects. It shouldn't break anything if you're careful about backward compatibility while writing/modifying your library. But yes it's very risky because if you do break something, it will be broken on all your projects.
2: Create a simple update script included with your library that copies the files from the main source to your project. Run this script when you update the library.
3: Use a version control setup. Git submodules might work ( http://book.git-scm.com/5_submodules.html )
4: Phar files are something I'd like to try. It's only drawback is that they don't work with PHP versions lower than 5.3
UPDATE: 04/12/2011 - I recommend using approach #3. Now that I've used it for a while, it seems to be clearly the best practice.
将共享代码签入版本控制存储库并将其签出到项目中不是更容易吗?例如,SVN 支持
svn:externals
属性Wouldn't it be easier to check the shared code into a version control repository, and check it out into the projects? For example, SVN supports that with the
svn:externals
property尽管 PHP 现在支持 phar 文件,但解决问题的正确方法是有效地使用包含文件(和路径)。
包含路径总是以相同的文件顺序搜索 - 因此一个好的做法是定义 2 个层次结构,一个用于代码的范围(例如,所有内容、应用程序、模块),另一个用于其运行的环境(保存相关 URL、batabase 连接等内容,例如所有内容、环境(开发/测试/实时)、集群节点)
C.
Although PHP now supports phar files, the right way to solve the problem is by using include files (and paths) effectively.
The include path is always searched in the same order for files - so a good practice is to to have 2 hierarchies defined, one for the scope of the code (e.g. everything, application, module) and one for the environment where it is running (holding such things as related URLs, batabase connections etc e.g. everything, environment (development / test / live), cluster node)
C.