PHP 预定义接口和SPL - 版本检查是否足够?

发布于 2024-10-29 23:32:57 字数 282 浏览 8 评论 0原文

这也许是一个显而易见的问题,但我想确定一下。

我正在尝试找出“预定义接口”出现在哪个版本中PHP。我的假设是 5.0.0,因为这是根据文档添加 SPL 接口的时候。

此外,这些是否可以通过配置或重新编译来禁用,或者可以安全地假设版本检查满足它们的存在?为什么它们甚至与 SPL 分开,区别在于它们永远无法关闭,而 SPL 可以关闭? (至少在 5.3.0 之前)

This is perhaps an obvious question, but I want to be sure.

I'm trying to work out in which version the "Predefined Interfaces" appeared in PHP. My assumption is 5.0.0, as this is when the SPL interfaces were added according to the docs.

Furthermore, can these ever be disabled via configuration or recompile, or can one safely assume a version check satisfies their presence? Why are they even separated from the SPL, is the differentiator just that these can't ever be turned off, whilst the SPL can be? (at least prior to 5.3.0)

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

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

发布评论

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

评论(3

回心转意 2024-11-05 23:32:57

您可以在此处找到每个 SPL 接口的可用版本。有些是5.0之后添加的。并且根据手册,从 PHP 5.3.0 开始,无法禁用 SPL 。

You can find since what version each SPL interface is available here. Some were added after 5.0. And according to the manual as of PHP 5.3.0 SPL can't be disabled.

极致的悲 2024-11-05 23:32:57

对于何时添加接口或差异是什么(在 PHP CVS 中挖掘五分钟并没有产生差异),我没有比您的假设更好的答案。

但是,有一种万无一失的方法来检查它们是否存在,而不是依赖于版本检查: interface_exists。对于 PHP 版本 5.0.0 和 5.0.1,您必须改为执行 class_exists

所以:

function interfaces_defined() {
    // Most likely case first
    if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
        return true;
    }
    else if (version_compare(PHP_VERSION, '5.0.0', '<')) {
        return false;
    }
    else if (version_compare(PHP_VERSION, '5.0.1', '<=')) {
        return class_exists('Iterator');
    }
    else {
        return interface_exists('Iterator');
    }
}

不是最漂亮的,但它绝对涵盖了所有基础。

I don't have a better answer than your assumptions for when the interfaces were added, or what the differentiator is (five minutes digging in the PHP CVS didn't produce one).

However, there is a bulletproof way to check for their existence instead of relying on a version check: interface_exists. For PHP versions 5.0.0 and 5.0.1, you will have to do class_exists instead.

So:

function interfaces_defined() {
    // Most likely case first
    if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
        return true;
    }
    else if (version_compare(PHP_VERSION, '5.0.0', '<')) {
        return false;
    }
    else if (version_compare(PHP_VERSION, '5.0.1', '<=')) {
        return class_exists('Iterator');
    }
    else {
        return interface_exists('Iterator');
    }
}

Not the prettiest, but it definitely covers all bases.

失眠症患者 2024-11-05 23:32:57

目前,手册仅显示属于每个接口的方法的版本信息。该版本信息可以在页面顶部方法原型上方找到(info)。

因此,单击方法描述并查看其中列出的内容。

这些可以通过配置或重新编译来禁用吗?或者可以安全地假设版本检查满足它们的存在吗?为什么它们甚至与 SPL 分开,区别在于它们永远无法关闭,而 SPL 可以关闭? (至少在5.3.0之前)

这些接口根本不是 SPL 扩展的一部分,并且无法启用或禁用它们。它们需要在 Zend 引擎中实现;不可能做他们在扩展中所做的事情,即 SPL。


不管怎样,我不久前提出了一个关于提供版本信息的功能请求 (#49927)在类(因此也是界面)概述/概要页面上。感谢您提醒我它存在并且很有用!

Currently, the manual only displays version information for the methods belonging to each interface. This version information can be found at the top of the page above the method prototype (info).

So, click through to the method description(s) and see what is listed there.

Can these ever be disabled via configuration or recompile, or can one safely assume a version check satisfies their presence? Why are they even separated from the SPL, is the differentiator just that these can't ever be turned off, whilst the SPL can be? (at least prior to 5.3.0)

Those interfaces are not part of the SPL extension at all and there is no enabling or disabling of them. They were required to be implemented in the Zend engine; it would not be possible to do what they do in an extension, which the SPL is.


For what it is worth, I opened a feature request (#49927) a while ago about making version information available on the class (and therefore also interface) overview/synopsis pages. Thanks for reminding me that it exists and would be useful!

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