|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69056 Typecasting resources to integers should be defined
Submitted: 2015-02-15 13:58 UTC Modified: 2015-02-24 00:28 UTC
Avg. Score:4.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: grzegorz129 at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.6.5 OS:
Private report: No CVE-ID: None
 [2015-02-15 13:58 UTC] grzegorz129 at gmail dot com
Typecasting resources to integers became common practice when used with stream_select(). It provides fast & consistent between versions method to locate sockets returned by stream_select(), however it's still undefined by PHP documentation (manual for integer type clearly warns about casting unknown types to integers: "The behaviour of converting to integer is undefined for other types. Do not rely on any observed behaviour, as it can change without notice.").
From some time now stream_select() preserves assoc. array keys (which enables usage of eg. local socket name as key), but it also cannot be considered stable due to doc bug #68798. 

Expected result:
Casting resources to integers should be documented. Casting resource to integer should produce unique number of resource (which is actual result, but not documented).

Actual result:
Casting resource to integer produces strict warning. Actual integer produced is the same as shown by var_dump().

class StreamServer#1 (1) {
    private $zeroStream =>
    resource(22) of type (Direct I/O File Descriptor)


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-02-16 00:34 UTC]
-Status: Open +Status: Feedback -Type: Feature/Change Request +Type: Bug -Package: *Programming Data Structures +Package: Scripting Engine problem
 [2015-02-16 00:34 UTC]
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.

I don't get any warnings.

Meanwhile, both the documentation and the spec say resources are converted to their number, so any warning produced would (probably) be a bug.
>If a resource is converted to an integer, then the result will be the unique
>resource number assigned to the resource by PHP at runtime.
>If the source is a resource, the result is the resource's unique ID.
 [2015-02-23 22:40 UTC] grzegorz129 at gmail dot com
You're right - it casts to integers just fine and I just missed that in documentation (I was looking for something related to streams, not exactly resources ;)).
To be curious I'm surprised it doesn't return actual FD# when casted to number. I started thinking that counting up without reusing number (which mostly will create mess if their aren't tightly tied to particular sockets like FDs) might be fatal at some point - unfortunately it ended as next bug report.

I think this report can be closed, and further investigation should be taken under bug #69109.
 [2015-02-24 00:28 UTC]
-Status: Feedback +Status: Not a bug
 [2015-02-24 00:28 UTC]
Getting the descriptor would be nice but I don't know if there's the capability of each resource type casting itself differently. There's been some interest in moving resources to objects so if that ever happens then casting becomes moot.

Saw that other bug. Failing at PHP_INT_MAX resources probably isn't a coincidence...
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 20 20:01:32 2024 UTC