PHP Predefined Interfaces & SPL - version check sufficient?

220 Views Asked by At

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)

3

There are 3 best solutions below

1
On BEST ANSWER

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.

1
On

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.

0
On

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!