简体   繁体   English

在PHP中创建共享库

[英]Creating Shared libraries in PHP

Im working on a few php based projects and im having an issue of having to maintain separate code bases with common code . 我正在从事一些基于php的项目,并且我有一个问题,那就是必须维护具有通用代码的单独代码库 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. 在Java中,我们可以创建一个库(jar文件)并在不同项目之间共享。 Is there any shared library related concept available in PHP 5. PHP 5中是否有任何与共享库相关的概念可用。

PHP的jar版本称为Phar: http//php.net/manual/zh/book.phar.php

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 例如,SVN通过svn:externals属性支持

Although PHP now supports phar files, the right way to solve the problem is by using include files (and paths) effectively. 尽管PHP现在支持phar文件,但是解决问题的正确方法是有效使用包含文件(和路径)。

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 (eg everything, application, module) and one for the environment where it is running (holding such things as related URLs, batabase connections etc eg everything, environment (development / test / live), cluster node) 始终以相同的顺序搜索include路径中的文件-因此,一个好的做法是定义2个层次结构,一个层次结构用于代码范围(例如,所有内容,应用程序,模块),另一个层次结构用于其运行环境(持有诸如相关URL,数据库连接之类的东西,例如一切,环境(开发/测试/实时),集群节点)

C. C。

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. 1:将包含路径设置为所有项目中的一个共享文件夹。 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. 2:创建一个包含在库中的简单更新脚本,该脚本会将文件从主源复制到项目中。 Run this script when you update the library. 更新库时,请运行此脚本。

3: Use a version control setup. 3:使用版本控制设置。 Git submodules might work ( http://book.git-scm.com/5_submodules.html ) Git子模块可能有效( http://book.git-scm.com/5_submodules.html

4: Phar files are something I'd like to try. 4:Phar文件是我想尝试的东西。 It's only drawback is that they don't work with PHP versions lower than 5.3 唯一的缺点是它们不适用于低于5.3的PHP版本

UPDATE: 04/12/2011 - I recommend using approach #3. 更新:2011年4月12日-我建议使用方法3。 Now that I've used it for a while, it seems to be clearly the best practice. 既然我已经使用了一段时间,那么这显然是最佳实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP无法加载共享库 - PHP cannot load shared libraries 使用 GCP PHP 客户端库创建文件夹 - Creating folders using GCP PHP Client Libraries 从PHP内部运行Rscript:无法加载共享库 - Running Rscript from within PHP: cannot load shared libraries PHP:加载共享库时出错:libmysqlclient.so.18 - PHP: error while loading shared libraries: libmysqlclient.so.18 创建具有共享结构和php函数的多语言wordpress网站 - Creating multilingual wordpress site with shared structure and php functions Google Drive PHP API 客户端:在共享驱动器中创建文件夹 - Google Drive PHP API Client: Creating a folder in a shared drive 本地开发和符号链接共享库 - Local development and symlinked shared libraries PHP Cron作业返回“-:加载共享库时出错:libc.so.6:文件末尾的ELF加载命令” - PHP Cron jobs return “-: error while loading shared libraries: libc.so.6: ELF load command past end of file” 为什么AWS Lambda自定义php运行时抛出错误“加载共享库时出错:libpng15.so.15:无法打开共享库文件:…”? - Why AWS lambda custom php run time throw the error “error while loading shared libraries: libpng15.so.15: cannot open shared object file:…”? 加载共享库时出现 xampp 错误 - xampp error while loading shared libraries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM