高级 PHP 设置 - 有选择地包括内置 PHP 函数

发布于 2024-10-07 10:04:48 字数 1454 浏览 0 评论 0原文

有大量内置 PHP 函数。我想知道,在担任软件工程师近两年半之后,我几乎没有使用过其中的一小部分。但所有这些都已定义并且可以与默认 PHP 安装一起使用。

我在 SO 中读到 PHP 提供了所有这些内置的东西,但是用 JAVA 等语言做类似的事情需要大量编码。这是正确的吗?我对其他语言没有太多经验。

另外,我是否正确地假设这些函数的很大一部分未被任何其他内置函数或任何东西(内部依赖项)使用?例如这些函数 pdf_fit_table(), gzopen()

如果是这样,那么作为高级程序员,PHP 是否为我们提供了任何选项来根据特定的项目需求或更动态地根据特定的模块选择性地加载它们?例如,仅当我有 PDF 相关任务时才加载 PDF 相关功能。如果可以的话,可以做到什么程度?如果在 PHP 安装级别,那么我认为在共享托管的情况下这是不可能的。是否有更好的解决方案?

我只是从常识的角度来说,我们根据需要包含包含函数的文件。

它会带来性能提升吗?

我对PHP的核心库等不太了解。所以,请透露一些信息。

更新:
感谢您的回答

@pygorex1 - HipHop 方法是整体优化 PHP。所以,用非常简单的术语来说,如果我是对的,如果在使用 HipHop 之前运行需要 1 秒,那么它可能会变成 0.7 秒。但在这两种情况下,这些额外的不必要的定义函数的存在增加了它们的开销(例如,第一种情况为 0.1 秒,在 HipHop 情况下为 0.07 秒)。如果是这样,那么 HipHop 的目标就是其他东西,并且不能回答我的问题。但是,您给出的另外两点表示所有操作都必须在编译时完成。所以,这可能意味着如果我使用扩展进行编译,那么每次都会加载该扩展下的函数组。那么可能没有进一步的方法来去除夹杂物?某种everride?

@Tyler - 我同意,做我所要求的事情可能很困难,但原因不是你所说的。找出依赖关系并不是那么困难。仅应用常识,我可以说诸如 is_numeric()is_array()array_walk()func_get_args() 之类的函数 等是非常基本的,可能被许多人调用,但是有一些容易区分的组,例如包含 socket_connect() 的套接字函数组,如果没有明确需要,则不需要包含它们。问题可能是需要在编译时指定,就像 pygorex1 已经回答的那样。

There are a hell lot of inbuilt PHP functions. I was wondering that after almost 2 and a half years of working as a software engineer I hardly use a little fraction of those. But all of them are defined and can be used with the default PHP installations.

I read somewhere in SO that PHP provides all these inbuilt things but doing similar things with languages like JAVA needs a lot of coding. Is that correct? I am not experienced in other languages much.

Also, am I correct to assume that a large portion of these functions are not used by any of the other inbuilt functions or anything (internal dependencies)? E.g. these functions pdf_fit_table(), gzopen() are needed only in case of PDF and gzip file related things respectively.

If so, then as advanced programmers, does PHP provide any option to us to selectively load them, based on the specific project requirements or more dynamically, based on a specific module? e.g. load PDF related functions only if I have PDF related tasks. If possible, at what level can it be done? If at the PHP installation level, then I think it is not possible in case of shared hosting. Is a better solution to this possible?

I am just speaking from a common sense point of view, we include files containing functions on a need basis.

Is it going to give a performance boost?

I am not much aware of the core libraries etc. of PHP. So, please shed some light.

Updates:
Thanks for the answers

@pygorex1 - The HipHop way is to optimize PHP overall. So, putting in very simple terms, if I am correct, if it was taking 1 second to run before then using HipHop it may make it 0.7 second. But in both the cases, the presence of those extra unnecessary defined functions are adding their overhead (say 0.1 second in first case and 0.07 sec in HipHop case). If so, then HipHop targets something else and does not answer my question. However, the other two points you gave say that all has to be done while compiling. So, it probably means if I compile with an extension then the function groups under that will be loaded every time . Then probably there is no further way of removing the inclusion? Some kind of everride?

@Tyler - I agree that it might be difficult to do what I am asking for but the reason is not what you are saying. It cannot be so difficult to find out the dependencies. Just applying common sense, I can say that functions like is_numeric(), is_array(), array_walk(), func_get_args() etc. are very basic ones and are probably called by many but there are easily distinguishable groups like the socket functions group containing e.g. socket_connect() which need not be included if not explicitly needed. The problem probably is that it needs to be specified while compiling, like pygorex1 has answered.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑梦风尘 2024-10-14 10:04:48

关于任何潜在的性能提升 - 您可能不会注意到它,除非您正在提供大量动态 PHP 页面。这条路以前有人走过 - 看看 HipHop,Facebook 的 PHP 优化工具C++。利用 APC 和 eAccelerator 等字节码缓存和/或重写 PHP 代码以使用 memcached 智能缓存将比启用/禁用某些 PHP 函数更能提高 PHP 性能。

话虽如此,有两种主要方法可以减少 PHP 可用函数的数量:

PHP 编译时选项

从源代码编译 PHP 时可用。问题 gzopen() 中提到的函数之一是 zlib 扩展的一部分,并且必须是 在编译时启用。有很多内置编译时选项

PHP 模块

这些模块由 PHP 动态加载,并由扩展名下的 php.ini 配置文件控制 - 它们是 Windows 上的 .dll 文件或 Linux 上的 .so 文件。我的开发 php.ini 的片段:

...
extension=php_bz2.dll
;extension=php_curl.dll
;extension=php_dba.dll
;extension=php_dblib.dll
extension=php_mbstring.dll
extension=php_exif.dll
extension=php_fileinfo.dll
extension=php_gd2.dll
...

Concerning any potential performance boost - you're probably not going to notice it unless you're serving a ton of dynamic PHP pages. This road has been traveled before - take a look at HipHop, Facebook's tool to optimize PHP into C++. Utilizing byte code caches like APC and eAccelerator AND/OR rewriting your PHP code to cache intelligently with memcached will improve PHP performance far more than enabling/disabling certain PHP functions.

That having been said, there's two main ways to pare down the number of functions that PHP has available:

PHP compile-time options

Available when compiling PHP from source. One of the functions noted in the question gzopen() is part of the zlib extension and has to be enabled at compile time. There's quite a few built-in compile-time options.

PHP modules

These are loaded dynamically by PHP and are controlled by the php.ini config file under extensions - they are .dll files on Windows or .so files on Linux. A snippet from my development php.ini:

...
extension=php_bz2.dll
;extension=php_curl.dll
;extension=php_dba.dll
;extension=php_dblib.dll
extension=php_mbstring.dll
extension=php_exif.dll
extension=php_fileinfo.dll
extension=php_gd2.dll
...
面如桃花 2024-10-14 10:04:48

dl() 用于在运行时加载 PHP 扩展。

动态加载扩展的示例:

if (!extension_loaded('sqlite')) {
    $prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '';
    dl($prefix . 'sqlite.' . PHP_SHLIB_SUFFIX);
}

这取自http:// /php.net/manual/en/function.dl.php

There is dl() to load a PHP extension at runtime.

Example to load an extension dynamically:

if (!extension_loaded('sqlite')) {
    $prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '';
    dl($prefix . 'sqlite.' . PHP_SHLIB_SUFFIX);
}

This is taken from http://php.net/manual/en/function.dl.php

删除会话 2024-10-14 10:04:48

php 函数命名空间崩溃,就是这样。

不,无法在运行时有选择地加载它们。仅仅因为你不调用某些东西,并不意味着你调用的东西没有调用它。

The php function namespace debacle, is, well, exactly that.

No, there's no way selectively load them at run-time. Just because you don't call something, doesn't mean something you call doesn't call it.

数理化全能战士 2024-10-14 10:04:48

不必费心编译内置函数。了解共享库和 Linux 缓存系统。这些文件(和函数)基本上总是被加载和缓存,因此对应用程序的影响很小。正如 pygorex1 所说,使用良好的缓存机制比故意破坏 PHP 发行版更好。

@powtac:使用 dl() 作为动态加载某些库的方式可能会实际减慢您的应用程序的速度(取决于您执行了多少 dl() ,这可能会更好让它们始终加载到内存中而不是根据请求加载它们)

@Tyler Eaves:您可以禁用某些函数的实际调用。不过,没有什么可以阻止它们的加载。

此外,据我所知,hip-hop 实际上将 php 代码编译为 C/C++ 代码,然后编译它。与脚本语言相比,这具有跳过虚拟机、特定于 php 的升级和大量开销的巨大优势,但有一个很大的缺点,即它不再是脚本语言。

Dont bother compiling out built-in functions. Learn about shared libraries and linux caching system. Those files(and functions) are basically always loaded and cached so it has very little impact on an application. As pygorex1 said, its better to use a good caching mechanism than crippling the PHP distribution on purpose.

@powtac: doing dl() as a way to dinamically load some libraries might acually slow down your app(depends on how many dl() you do, it might be better to have them always loaded in memory than loading them on request)

@Tyler Eaves: you may disable some function from being called actually. There's nothing preventing their loading though..

Also, hip-hop as far as i knwo actually compiles php code down to C/C++ code, and then compiles it. This has the BIG advantage of skipping the virtual machine, and php-specific upcodes and lots of overhead over a scripted language, but has the big disatvantage that its not a scripted language anymore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文