PHP 7 and strict "resource" types

PhpPhp 7

Php Problem Overview


Does PHP 7 support strict typing for resources? If so, how?

For example:

	declare (strict_types=1);

	$ch = curl_init ();
	test ($ch);

	function test (resource $ch)
	{

	}

The above will give the error:

> Fatal error: Uncaught TypeError: Argument 1 passed to test() must be an instance of resource, resource given

A var_dump on $ch reveals it to be resource(4, curl), and the manual says curl_init () returns a resource.

Is it at all possible to strictly type the test() function to support the $ch variable?

Php Solutions


Solution 1 - Php

PHP does not have a type hint for resources because

> No type hint for resources is added, as this would prevent moving from resources to objects for existing extensions, which some have already done (e.g. GMP).

However, you can use is_resource() within the function/method body to verify the passed argument and handle it as needed. A reusable version would be an assertion like this:

function assert_resource($resource)
{
    if (false === is_resource($resource)) {
        throw new InvalidArgumentException(
            sprintf(
                'Argument must be a valid resource type. %s given.',
                gettype($resource)
            )
        );
    }
}

which you could then use within your code like that:

function test($ch)
{
    assert_resource($ch);
    // do something with resource
}

Solution 2 - Php

resource is not a valid type so it's assumed to be a class name as per good old PHP/5 type hints. But curl_init() does not return an object instance.

As far as I know there's not way to specify a resource. It probably wouldn't be so useful since not all resources are identical: a resource generated by fopen() would be useless for oci_parse().

If you want to check the resource in the function body, you can use get_resource_type() (with is_resource() to prevent errors), as in:

is_resource($ch) && get_resource_type($ch) === 'curl'

Starting on PHP/8.0, curl_init() returns an object so you can now use CurlHandle as type hint,

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhilView Question on Stackoverflow
Solution 1 - PhpGordonView Answer on Stackoverflow
Solution 2 - PhpÁlvaro GonzálezView Answer on Stackoverflow