php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78481 Can't use resource as typed param
Submitted: 2019-08-31 08:26 UTC Modified: 2019-08-31 18:55 UTC
From: momchil at bojinov dot info Assigned:
Status: Duplicate Package: *General Issues
PHP Version: 7.4.0beta4 OS: Windows 10
Private report: No CVE-ID: None
 [2019-08-31 08:26 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-08-31 08:46 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2019-08-31 08:46 UTC] 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:56 UTC] 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 18:55 UTC] requinix@php.net
-Status: Not a bug +Status: Duplicate
 [2019-08-31 18:55 UTC] requinix@php.net
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.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Dec 12 16:01:24 2019 UTC