summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2020-04-21 12:31:17 +0200
committerNikita Popov <nikita.ppv@gmail.com>2020-05-11 12:04:16 +0200
commita0abc26ef78da4209b937fefd0e94b2acf147cef (patch)
tree4cc58d850673cf0f18ad4691d3c9aa1f0f5bc6c9
parentd5d99ce8d1a81839f9e7c50b5534abf514384dcf (diff)
downloadphp-git-a0abc26ef78da4209b937fefd0e94b2acf147cef.tar.gz
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.
-rw-r--r--UPGRADING4
-rw-r--r--Zend/tests/get_resource_id.phpt18
-rw-r--r--Zend/zend_builtin_functions.c14
-rw-r--r--Zend/zend_builtin_functions.stub.php2
-rw-r--r--Zend/zend_builtin_functions_arginfo.h6
5 files changed, 44 insertions, 0 deletions
diff --git a/UPGRADING b/UPGRADING
index 5380b1058d..c6dff51274 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -570,6 +570,10 @@ PHP 8.0 UPGRADE NOTES
6. New Functions
========================================
+- Core:
+ . Added get_resource_id($resource) function, which returns the same value as
+ (int) $resource. It provides the same functionality under a clearer API.
+
- PCRE:
. Added preg_last_error_msg(), which returns a human-readable message for
the last PCRE error. It complements preg_last_error(), which returns an
diff --git a/Zend/tests/get_resource_id.phpt b/Zend/tests/get_resource_id.phpt
new file mode 100644
index 0000000000..70b3995d46
--- /dev/null
+++ b/Zend/tests/get_resource_id.phpt
@@ -0,0 +1,18 @@
+--TEST--
+get_resource_id() function
+--FILE--
+<?php
+
+$file = fopen(__FILE__, 'r');
+
+// get_resource_id() is equivalent to an integer cast.
+var_dump(get_resource_id($file) === (int) $file);
+
+// Also works with closed resources.
+fclose($file);
+var_dump(get_resource_id($file) === (int) $file);
+
+?>
+--EXPECT--
+bool(true)
+bool(true)
diff --git a/Zend/zend_builtin_functions.c b/Zend/zend_builtin_functions.c
index a8cc127683..9062cf6f5c 100644
--- a/Zend/zend_builtin_functions.c
+++ b/Zend/zend_builtin_functions.c
@@ -1495,6 +1495,20 @@ ZEND_FUNCTION(get_resource_type)
}
/* }}} */
+/* {{{ proto int get_resource_id(resource res)
+ Get the resource ID for a given resource */
+ZEND_FUNCTION(get_resource_id)
+{
+ zval *resource;
+
+ ZEND_PARSE_PARAMETERS_START(1, 1)
+ Z_PARAM_RESOURCE(resource)
+ ZEND_PARSE_PARAMETERS_END();
+
+ RETURN_LONG(Z_RES_HANDLE_P(resource));
+}
+/* }}} */
+
/* {{{ proto array get_resources([string resource_type])
Get an array with all active resources */
ZEND_FUNCTION(get_resources)
diff --git a/Zend/zend_builtin_functions.stub.php b/Zend/zend_builtin_functions.stub.php
index 43b55b42a7..9dfaf64ca5 100644
--- a/Zend/zend_builtin_functions.stub.php
+++ b/Zend/zend_builtin_functions.stub.php
@@ -91,6 +91,8 @@ function get_defined_vars(): array {}
function get_resource_type($res): string {}
+function get_resource_id($res): int {}
+
function get_resources(string $type = UNKNOWN): array {}
function get_loaded_extensions(bool $zend_extensions = false): array {}
diff --git a/Zend/zend_builtin_functions_arginfo.h b/Zend/zend_builtin_functions_arginfo.h
index 571b14ba72..728a76cdee 100644
--- a/Zend/zend_builtin_functions_arginfo.h
+++ b/Zend/zend_builtin_functions_arginfo.h
@@ -155,6 +155,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_get_resource_type, 0, 1, IS_STRI
ZEND_ARG_INFO(0, res)
ZEND_END_ARG_INFO()
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_get_resource_id, 0, 1, IS_LONG, 0)
+ ZEND_ARG_INFO(0, res)
+ZEND_END_ARG_INFO()
+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_get_resources, 0, 0, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, type, IS_STRING, 0)
ZEND_END_ARG_INFO()
@@ -244,6 +248,7 @@ ZEND_FUNCTION(get_declared_interfaces);
ZEND_FUNCTION(get_defined_functions);
ZEND_FUNCTION(get_defined_vars);
ZEND_FUNCTION(get_resource_type);
+ZEND_FUNCTION(get_resource_id);
ZEND_FUNCTION(get_resources);
ZEND_FUNCTION(get_loaded_extensions);
ZEND_FUNCTION(get_defined_constants);
@@ -305,6 +310,7 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(get_defined_functions, arginfo_get_defined_functions)
ZEND_FE(get_defined_vars, arginfo_get_defined_vars)
ZEND_FE(get_resource_type, arginfo_get_resource_type)
+ ZEND_FE(get_resource_id, arginfo_get_resource_id)
ZEND_FE(get_resources, arginfo_get_resources)
ZEND_FE(get_loaded_extensions, arginfo_get_loaded_extensions)
ZEND_FE(get_defined_constants, arginfo_get_defined_constants)