php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71518 Return type hinting for resources
Submitted: 2016-02-03 22:55 UTC Modified: 2016-02-03 23:06 UTC
Votes:3
Avg. Score:3.7 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:1 (33.3%)
From: almamu at almamu dot com Assigned:
Status: Suspended Package: Variables related
PHP Version: 7.0.2 OS: Ubuntu 12.04
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2016-02-03 22:55 UTC] almamu at almamu dot com
Description:
------------
The return type hinting is not working as expected, in this case I want to return a "resource" (such as the ones returned by curl functions, mysql functions, etc) but PHP thinks I must return an instance of an (non-existant) "resource" class, which breaks the purpose of the return type hinting in this function, the documentation on this lacks some specific information for some php types (like mixed, object and resource), so I may be making a mistake here (and probably too much assumptions).

Test script:
---------------
<?php
declare(strict_types=1);
function prepareCURLResource(string $url) : resource
{
    $curl = curl_init($url);

    curl_setopt($curl, CURLOPT_COOKIEFILE, "/tmp");
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 3);

    return $curl;
}

$res = prepareCURLResource("http://localhost");

echo htmlentities(curl_exec($curl));

Expected result:
----------------
The source code of the given URL (http://localhost) in this case.

Actual result:
--------------
[Wed Feb 03 22:46:42 2016] [warn] [client 192.168.100.2] mod_fcgid: stderr: PHP Fatal error:  Uncaught TypeError: Return value of prepareCURLResource() must be an instance of resource, resource returned in /var/www/test.php:12
[Wed Feb 03 22:46:42 2016] [warn] [client 192.168.100.2] mod_fcgid: stderr: Stack trace:
[Wed Feb 03 22:46:42 2016] [warn] [client 192.168.100.2] mod_fcgid: stderr: #0 /var/www/test.php(15): prepareCURLResource('http://localhost')
[Wed Feb 03 22:46:42 2016] [warn] [client 192.168.100.2] mod_fcgid: stderr: #1 {main}
[Wed Feb 03 22:46:42 2016] [warn] [client 192.168.100.2] mod_fcgid: stderr:   thrown in /var/www/test.php on line 12


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-02-03 22:57 UTC] almamu at almamu dot com
Whopsie, wrote a fast example and made a mistake on one of the var names. The last line of the example should be:
echo htmlentities(curl_exec($res));

Although, this does not change the current result of the code as it stops at the $res declaration
 [2016-02-03 23:06 UTC] requinix@php.net
-Status: Open +Status: Suspended
 [2016-02-03 23:06 UTC] requinix@php.net
IIRC resources were specifically not considered because of two main reasons:
1. "resource" alone isn't helpful: you could have a file handle or GD image or mysqli connection handle with prepareCURLResource() and PHP wouldn't know that you're using the wrong type of value.
2. resources are old and really should be converted into classes and objects instead. GMP is an example. That would, of course, take quite a bit of work, plus there's the logistics of how to handle things like is_resource().

Check the internals mailing list for discussions about this stuff.

RFCs:
- https://wiki.php.net/rfc/scalar_type_hints_v5
- https://wiki.php.net/rfc/return_types
- https://wiki.php.net/rfc/resource_typehint
 [2016-02-03 23:21 UTC] almamu at almamu dot com
1. Yeah, didn't think about that kind of use-case
2. In this case the curl extension should be updated, right? (or create a wrapper over normal CURL functions) As far as I can remember the extension does not have any class-based solution for this use-case

I did find the second RFC before opening the bug report, but didn't find anything about the first one (that clearly states why this example fails) 

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

Still, the last RFC you linked would go against that last statement. I'll keep an eye on that last RFC until the poll is done.

Thanks for your time and sorry for the hassle.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 01:01:28 2024 UTC