php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #81450 object type hint does not accept resource
Submitted: 2021-09-16 22:58 UTC Modified: 2021-09-16 23:13 UTC
From: gib-o-master at mail dot ru Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 8.0.10 OS:
Private report: No CVE-ID: None
 [2021-09-16 22:58 UTC] gib-o-master at mail dot ru
Description:
------------
i do proc_open() which returns resource https://www.php.net/manual/en/function.proc-open


$proc = proc_open(..);
$hand = new ProcessController($proc);


i pass it to the constructor function which has object type hint


function __construct(public object $proc)
{..}


the result is "($proc) must be of type object, resource given"
why object hint does not accept resource? will it be fixed in future versions of php?



Expected result:
----------------
resource is accepted

Actual result:
--------------
TypeError exception

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-09-16 23:11 UTC] requinix@php.net
-Status: Open +Status: Not a bug -Type: Feature/Change Request +Type: Bug
 [2021-09-16 23:13 UTC] danack@php.net
-Type: Bug +Type: Feature/Change Request
 [2021-09-16 23:13 UTC] danack@php.net
> why object hint does not accept resource?

Because resources are not objects. Resources are their own type and so don't pass the object type check.


> will it be fixed in future versions of php?

Yes, but not by making resource pass the object type check.

There is an on-going effort to replace all resource types with opaque objects. There is a list of the progress here: https://github.com/php/php-tasks/issues/6

So at some point proc_open(..) will be changed from returning a resource, to returning a 'Proc' or 'Process' object.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 11:01:30 2024 UTC