Skip to content

Commit a0abc26

Browse files
committed
Add get_resource_id() function
Behavior is same as for (int) $resource, just under a clearer name. Also type-safe, in that the parameter actually needs to be a resource. Closes GH-5427.
1 parent d5d99ce commit a0abc26

5 files changed

+44
-0
lines changed

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,10 @@ PHP 8.0 UPGRADE NOTES
570570
6. New Functions
571571
========================================
572572

573+
- Core:
574+
. Added get_resource_id($resource) function, which returns the same value as
575+
(int) $resource. It provides the same functionality under a clearer API.
576+
573577
- PCRE:
574578
. Added preg_last_error_msg(), which returns a human-readable message for
575579
the last PCRE error. It complements preg_last_error(), which returns an

Zend/tests/get_resource_id.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
get_resource_id() function
3+
--FILE--
4+
<?php
5+
6+
$file = fopen(__FILE__, 'r');
7+
8+
// get_resource_id() is equivalent to an integer cast.
9+
var_dump(get_resource_id($file) === (int) $file);
10+
11+
// Also works with closed resources.
12+
fclose($file);
13+
var_dump(get_resource_id($file) === (int) $file);
14+
15+
?>
16+
--EXPECT--
17+
bool(true)
18+
bool(true)

Zend/zend_builtin_functions.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,20 @@ ZEND_FUNCTION(get_resource_type)
14951495
}
14961496
/* }}} */
14971497

1498+
/* {{{ proto int get_resource_id(resource res)
1499+
Get the resource ID for a given resource */
1500+
ZEND_FUNCTION(get_resource_id)
1501+
{
1502+
zval *resource;
1503+
1504+
ZEND_PARSE_PARAMETERS_START(1, 1)
1505+
Z_PARAM_RESOURCE(resource)
1506+
ZEND_PARSE_PARAMETERS_END();
1507+
1508+
RETURN_LONG(Z_RES_HANDLE_P(resource));
1509+
}
1510+
/* }}} */
1511+
14981512
/* {{{ proto array get_resources([string resource_type])
14991513
Get an array with all active resources */
15001514
ZEND_FUNCTION(get_resources)

Zend/zend_builtin_functions.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ function get_defined_vars(): array {}
9191

9292
function get_resource_type($res): string {}
9393

94+
function get_resource_id($res): int {}
95+
9496
function get_resources(string $type = UNKNOWN): array {}
9597

9698
function get_loaded_extensions(bool $zend_extensions = false): array {}

Zend/zend_builtin_functions_arginfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_get_resource_type, 0, 1, IS_STRI
155155
ZEND_ARG_INFO(0, res)
156156
ZEND_END_ARG_INFO()
157157

158+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_get_resource_id, 0, 1, IS_LONG, 0)
159+
ZEND_ARG_INFO(0, res)
160+
ZEND_END_ARG_INFO()
161+
158162
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_get_resources, 0, 0, IS_ARRAY, 0)
159163
ZEND_ARG_TYPE_INFO(0, type, IS_STRING, 0)
160164
ZEND_END_ARG_INFO()
@@ -244,6 +248,7 @@ ZEND_FUNCTION(get_declared_interfaces);
244248
ZEND_FUNCTION(get_defined_functions);
245249
ZEND_FUNCTION(get_defined_vars);
246250
ZEND_FUNCTION(get_resource_type);
251+
ZEND_FUNCTION(get_resource_id);
247252
ZEND_FUNCTION(get_resources);
248253
ZEND_FUNCTION(get_loaded_extensions);
249254
ZEND_FUNCTION(get_defined_constants);
@@ -305,6 +310,7 @@ static const zend_function_entry ext_functions[] = {
305310
ZEND_FE(get_defined_functions, arginfo_get_defined_functions)
306311
ZEND_FE(get_defined_vars, arginfo_get_defined_vars)
307312
ZEND_FE(get_resource_type, arginfo_get_resource_type)
313+
ZEND_FE(get_resource_id, arginfo_get_resource_id)
308314
ZEND_FE(get_resources, arginfo_get_resources)
309315
ZEND_FE(get_loaded_extensions, arginfo_get_loaded_extensions)
310316
ZEND_FE(get_defined_constants, arginfo_get_defined_constants)

0 commit comments

Comments
 (0)