PHP 8.2: Added imap_is_open function

Version8.2
TypeNew Feature

The IMAP extension in PHP 8.2.1 adds a new function named imap_is_open, that returns whether a given IMAP\Connection object is open or not.

Prior to adding this function, there was no way to determine if an IMAP\Connection object is open. Attempting to use a closed object results in a \ValueError exception, so the new imap_is_open function provides a way to check a given connection object without having to use a try/catch block.

imap_is_open is only available in PHP 8.2.1 later Note that the imap_is_open function was added in PHP 8.2.1. This function does not exist in PHP 8.2.0.

imap_is_open function synopsis

function imap_is_open(IMAP\Connection $imap): bool {}

imap_is_open function is declared in the global namespace.

User-land PHP Implementation

Because the imap_is_open function returns internal object data, it is not possible to port this function to older PHP versions. However, because all IMAP functions that accept an IMAP\Connection object throws an exception, a close implementation in user-land PHP with the help of imap_ping function might be the second best solution.

if (!function_exists('imap_is_open')) {
  function imap_is_open(IMAP\Connection $imap): bool {
    try {
      imap_ping($imap);
      return true;
    }
    catch (\ValueError $exception) {
      return false;
    }
  }
}

Backwards Compatibility Impact

imap_is_open is a new function declared by the IMAP extension in the global namespace. Unless a PHP application declares a function with the same name in the global namespace, this change should not introduce any compatibility issues.

While not being the same, it is possible to port a similar (but not identical in functionality) to older PHP versions. Otherwise, catching the \ValueError exceptions is the viable cross-version compatible approach.

Related Changes


Implementation