IsResource#

Packagist GitLab GitHub Bitbucket Gitea

The library provides a version of is_resource() and get_resource_type() functions that can understand opaque objects. It is useful tool if you need to support a library that may be affected by the resource to object migration.

Installation#

composer require 'arokettu/is-resource'

Documentation#

Functions#

is_resource()#

<?php

function \Arokettu\IsResource\is_resource(mixed $value): bool;

The function returns true if $value is

  • a resource

  • an opaque object that was a resource in earlier PHP versions

get_resource_type()#

<?php

function \Arokettu\IsResource\get_resource_type(resource|object $resource): string;

The function returns the resource type string if $resource is

  • a resource

  • an opaque object that was a resource in earlier PHP versions

In other cases the behavior falls back to the default \get_resource_type() behavior:

  • in PHP < 8.0: return null + issue E_WARNING

  • in PHP >= 8.0: throw TypeError

try_get_resource_type()#

<?php

function \Arokettu\IsResource\try_get_resource_type(mixed $resource): string|null;

A useful shortcut to check the resource type. It returns null in case the resource was not recognized.

<?php

use function Arokettu\IsResource\try_get_resource_type;

// was:
if (is_resource($conn) && get_resource_type($conn) === 'pgsql link') {
    // ...
}

// With PHP 8.1 this transforms to:
if (is_resource($conn) && get_resource_type($conn) === 'pgsql link' || $conn instanceof PgSql\Connection) {
    // ...
}

// but with try_get_resource_type() from this library it's just this:
if (try_get_resource_type($conn) === 'pgsql link') {
    // ...
}

Supported Extensions#

List of the supported extensions#

This a list of the supported extensions with class names that this library recognizes.

Converted in PHP 5.6#
  • gmp (GMP)

Converted in PHP 7.2#
  • hash (HashContext)

Converted in PHP 8.0#
  • curl (CurlHandle, CurlMultiHandle, CurlShareHandle)

  • enchant (EnchantBroker, EnchantDictionary)

  • gd (GdImage),

  • openssl (OpenSSLAsymmetricKey, OpenSSLCertificate, OpenSSLCertificateSigningRequest)

  • shmop (Shmop)

  • sockets (AddressInfo, Socket)

  • sysvmsg (SysvMessageQueue)

  • sysvsem (SysvSemaphore)

  • sysvshm (SysvSharedMemory)

  • xml (XMLParser)

  • xmlrpc (XmlRpcServer)

  • xmlwriter (XMLWriter)

  • zlib (DeflateContext, InflateContext)

Converted in PHP 8.1#

Note

pspell is not covered by this lib, see below

  • fileinfo (finfo)

  • ftp (FTP\Connection)

  • imap (IMAP\Connection)

  • ldap (LDAP\Connection, LDAP\Result, LDAP\ResultEntry)

  • pgsql (PgSql\Connection, PgSql\Result, PgSql\Lob)

Special cases#

PSpell#

The PSpell extension is not covered by this library because the PHP documentation is misleading. The changelog currently states this:

Version

Description

8.1.0

Returns an PSpell\Dictionary instance now; previously, a resource was returned.

But if you try to use pspell_new() or pspell_config_create() in earlier PHP, you can notice that they return int, not resource. Therefore return values of these functions could never have been checked by is_resource() / get_resource_type().

Submitting a missing resource#

Note

Please keep in mind that the minimum supported version is PHP 5.3 so short array syntax and ::class constants are not available!

The main data file is data/object_maps.php. It has the following structure:

<?php

return array(
    // Top level keys: PHP version where the change was made in the PHP_VERSION_ID form
    70200 => array(
        // Second level keys: extension names as used in extension_loaded() for example
        'hash' => array(
            // Third level:
            // key is a class name after the change
            // value is a resource string before the change
            'HashContext' => 'Hash Context'
        ),
    ),
);

After you added or fixed the data file, run the generator:

php sbin/build_resource_map_class.php

This will update the generated data classes in the gen/ directory.

License#

The library is available as open source under the terms of the MIT License.