了解 PHP 库安装
我试图了解与在系统 (Linux/OSX) 上“安装”PHP 库相关的选项和术语
以下是我试图涵盖的一些具体要点:
- 与 PHP 库相关时,“安装”意味着什么?
- 库和扩展之间有什么区别?
- php.ini 是如何融入的?
- PEAR 如何融入其中?
- 如何导入/包含库?
- 如果我创建自己的库,打包和分发它的最佳方式是什么?
谢谢-对多部分感到抱歉......
I'm trying to understand the options and terminology relating to "installing" PHP libraries on a system (Linux/OSX)
Here are some specific points I'm trying to cover:
- What does "install" mean when relating to PHP libraries?
- What's the difference between libraries and extensions?
- How does php.ini fit in?
- How does PEAR fit in?
- How do you import/include libraries?
- If I create my own library, what's the best way to package and distribute it?
Thanks- and sorry for the multi-part...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几点...
PHP 没有本机“导入”基础设施,如 python、java 或 .net。在 PHP 中使用库的方法有多种。
将它们编译成 PHP 二进制文件。这是最高级的方法,除非您有非常特殊的需求,否则通常不需要。
将它们作为 PHP 模块安装在服务器上,并将它们包含在 PHP.ini 中。从 PHP 程序员的角度来看,这些扩展是 PHP 的一部分——始终可用。添加和删除它们更加容易,而无需重建 PHP 本身。
在服务器上的某个位置安装 PHP 代码,并将其 include() 到您的 PHP 脚本中。
将库的副本保存到您的项目中,并将其包含到您的 PHP 脚本中。
--
在基本层面上,代码要么是解释器的一部分(静态或动态),要么是被 include() 到项目中的普通旧 PHP 代码。
出于您的目的,我只能建议您坚持使用行业标准的 PHP 发行版(选择一个好的 Linux 操作系统,并使用它的 PHP)。然后,您在解释器级别所需的几乎所有库都可以作为附加包提供,而其复杂性则留给每天这样做的人。
在 RedHat/Centos 上,您可以运行:
yum install php php-memcached php-gd php-pecl
--
对于您可能想要使用的所有其他类型的库,最好采用一个好的 PHP 框架来为您处理所有这些事情。
一些例子是:
(不按任何顺序,只是如果
您已经采取了使用 RPM 或类似方法来管理 PHP 和扩展方面的编译的标准方法,那么一个好的可靠框架将负责包含您需要的所有附加 PHP 库代码。
最终的结果是,您专注于交付产品,而不是您必须学习和发明的所有基础设施。
--
php.ini 在 PHP 启动时被解析并运行(每次用于命令行,每个服务器在 apache 中启动一次)。它定义了很多设置,包括很多模块,配置这些模块等等...
您实际上可以使用 PHP 中的 ini_set() 函数覆盖 php.ini 中的一些设置。然而,这仅对某些设置有效。其他需要在脚本启动之前设置。
在 apache 下运行时,您可以向
.htaccess
和
指令添加行,这些指令完全覆盖该目录/虚拟主机的 PHP.ini。(请纠正我的语法,如果有错误请删除此注释)
--
为了回答关于您自己的库以及打包它的最佳方式的第 6 个问题,我建议您首先评估该库的需求。如果你真的想做某件事,那就找出人们做这件事最常见的方式。如果它是一个简单的库,那么一个带有漂亮网站的 .php 文件就足够了。
--
也许有点漫无目的,但我希望这能为您指明正确的方向。
A couple points...
PHP has no native "import" infrastructure, like python, java, or .net. There are several ways that libraries can be used in PHP.
compile them into the PHP binary. This is the most advanced way, and not usually desirable unless you have very special needs.
Install them as PHP modules on the server, and include them in PHP.ini. From the point of view of the PHP programmer, these extensions are part of PHP -- always available. It's just easier to add and remove them without having to rebuild PHP itself.
Install PHP code on the server somewhere, and include() it into your PHP script.
Save a copy of a library into your project, and include it into your PHP script.
--
At a basic level, code is either part of the interpreter (static or dynamic), or it is plain old PHP code that is include()ed into your project.
For your purposes, I can only suggest that you stick with an industry standard PHP distribution (pick a good linux OS, and use it's PHP). Then almost all the libraries you will need at the interpreter level are available as add-on packages, and the complexity of that is left up to those who do it every day.
On RedHat/Centos, you might run:
yum install php php-memcached php-gd php-pecl
--
Regarding all the other kinds of libraries that you might want to use, it's most likely best to adopt a good PHP framework which takes care of all that for you.
Some examples are:
(not in any order, just ones that came to mind)
Provided that you have taken the standard approach of using RPM's or similar to manage the compiled in aspects of PHP and extensions, then a good solid framework will take care of the inclusion of all your additional PHP library code you need.
What the end result is, is that you focus on delivering a product, and not on all the infrastructure that you would otherwise have to learn and invent.
--
php.ini is parsed and run when PHP starts (each time for command line, once per server start in apache). It defines a lot of settings, includes a lot of modules, configures those modules, etc...
You can actually override some settings in php.ini with the ini_set() function in PHP. However, this is only effective for some settings. Others need to be set before your script starts.
When running under apache, you can add lines to
.htaccess
and<VirtualHost>
directives which totally override PHP.ini for that directory/virtual host.(please correct my syntax and remove this note if it is wrong)
--
In response to your #6 question about your own library and the best way to package it, I suggest you first evaluate the need of the library. And if you really are on to something, then find out the most common way people are doing it. If it is a simple library, then a .php file with a nice website would be sufficient.
--
Maybe a bit rambling, but I hope this points you in the right direction.
全部与 PHP 直接相关:
require(_once)
或include(_once)
。对于课程,您可以设置自动加载器。请参阅 PHP 手册。如果您想编写 PHP 库并想要一种简单的打包和分发方法,那么可以看看 PEAR。
All directly related to PHP:
require(_once)
orinclude(_once)
. For classes you can set up a autoloader. Refer to the PHP manual.If you want to write PHP libraries and want a simple way for packaging and distribution, than have a look at PEAR.