Bug #78481 [Nab->Dup]: Can't use resource as typed param

From: Date: Sat, 31 Aug 2019 18:55:39 +0000
Subject: Bug #78481 [Nab->Dup]: Can't use resource as typed param
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-222512@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78481&edit=1 ID: 78481 Updated by: requinix@php.net Reported by: momchil at bojinov dot info Summary: Can't use resource as typed param -Status: Not a bug +Status: Duplicate Type: Bug Package: *General Issues Operating System: Windows 10 PHP Version: 7.4.0beta4 Block user comment: N Private report: N New Comment: The "must be an instance of resource, resource given" confusion sounds familiar but I can't find an existing bug for it. I'm not sure it's easily fixable anyways as what's happening is that PHP is trying to get a variable that's an instance of a class ("resource") and it found a non-object variable type (a resource) instead - the error message just fills in a "must be an instance of $expected, $actual given" template. Anyway, I did find bug #71518 from 2016 where I gave a more helpful answer. Previous Comments: ------------------------------------------------------------------------ [2019-08-31 08:56:32] momchil at bojinov dot info I get that. I just find the wording of the error ambiguous Why would resource even be accepted as a type ? ------------------------------------------------------------------------ [2019-08-31 08:46:08] requinix@php.net resource is not available as for type declarations because it says nothing about what type of resource it is. Your function would allow GD resources, but also file streams and cURL handles and other things you don't want. The type would be a false sense of security and so is not supported. ------------------------------------------------------------------------ [2019-08-31 08:26:13] momchil at bojinov dot info Description: ------------ I am trying to use resource for parameter type but I get TypeError Test script: --------------- $Picture = imagecreatetruecolor(100, 100); class bla { public function forPChart(resource $picture, $X, $Y) { $this->createImage($picture); } } (new bla())->forPChart($Picture, 0 ,0); Expected result: ---------------- Actual result: -------------- Fatal error: Uncaught TypeError: Argument 1 passed to bla::forPChart() must be an instance of resource, resource given, called in E:\xxx\test.php on line 13 and defined in E:\xxx\test.php:7 Stack trace: #0 E:\xxx\test.php(13): bla->forPChart(Resource id #4, 0, 0) #1 {main} thrown in E:\xxx\test.php on line 7 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=78481&edit=1

« previous php.bugs (#222512) next »