3

I'm currently investigating some PHP-code and I'm trying to make it more up to date. I found a function, which retrieves a filehandle (via fopen).

The returntype is stream. I checked it with

$handle = fopen("myfile.csv", "r");
var_dump(get_resource_type($handle));

As mentioned, the type seems to be stream.

When I try to pass this object to a function, I can't force the type with stream.

public function parseHandle(stream $handle){}

ends up with this error-message:

Argument 1 passed to parseHandle must be an instance of stream, resource given.

When I try to change the signature to function parseHandle(resource $resource) it yields the following error:

Argument 1 passed to parseHandle must be an instance of resource, resource given

What is the correct datatype and how should I use it in the arguments?

I know I can use is_resource in the very first lines of my function to verify it, but I still wonder.

DasSaffe
  • 2,080
  • 1
  • 28
  • 67
  • 1
    An alternative solution could be wrapping the code in a helper class. You could pass that class as an argument then. – gradosevic Nov 16 '16 at 19:37

2 Answers2

7

Unfortunately, you cannot use the primitive type resource as a type hint. The latest type hint system change to PHP, scalar type hints, only added int, float, string, and bool.

The error you quote has come up before as confusing, especially in the context of the scalar type hints. Let me template it for you:

Argument [N] passed to [Function] must be an instance of [Class], [Type] given

The confusion arises because PHP allows classes to have the same name as documented primitives (bool, float, etc.) for all primitives before PHP 7, and for some in PHP 7 and later . So when you say stream $handle, PHP is expecting $handle to be of class stream. Likewise resource $handle expects $handle to be of class resource.

If you want to type hint resources, I suggest using an \SplFileObject:

$handle = new \SplFileObject('myfile.csv', 'r');
function parseHandle(\SplFileObject $handle) { ... }

This is not the best thing in the world, as \SplFileObject has a few quirks, but at the end of the day, if you want to type hint it in PHP, you must either have an array, a scalar, or a class.

bishop
  • 37,830
  • 11
  • 104
  • 139
  • 2
    Is there a RFC for adding `resource` to the allowed types hinting? – Xenos Aug 18 '19 at 16:04
  • @Xenos To my knowledge, no. Were there one, I would vote yes, unless there was some strenuous technical argument against. – bishop Aug 18 '19 at 16:35
  • That would be a bad idea. Instead, PHP should more actively move to convert its core (extensions) to OOP, so that you can use proper (abstract/interface) class names for type hints. The usage of the `\SplFileObject` here abbove is a great example, but I would more prefer to see something like `\Php\Io\Stream` etc. – DennisP Dec 07 '21 at 13:57
0

One way to do type checking for a resource is to do it as the first statement in a function / method.

public function close($value)
{
    if (!is_resource($value)) {
        throw new InvalidArgumentException("The argument to this function must be a [blah] resource.");
    }

    .
    .
    .
}

Behold, the SPL Exception: InvalidArgumentException.

Anthony Rutledge
  • 6,980
  • 2
  • 39
  • 44