]> granicus.if.org Git - php/commitdiff
Add get_resource_id() function
authorNikita Popov <nikita.ppv@gmail.com>
Tue, 21 Apr 2020 10:31:17 +0000 (12:31 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Mon, 11 May 2020 10:04:16 +0000 (12:04 +0200)
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.

UPGRADING
Zend/tests/get_resource_id.phpt [new file with mode: 0644]
Zend/zend_builtin_functions.c
Zend/zend_builtin_functions.stub.php
Zend/zend_builtin_functions_arginfo.h

index 5380b1058d58ba8abe184ad8f8ebf0110d92dfe6..c6dff51274bd28553c696871873e13b14d6033b1 100644 (file)
--- 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 (file)
index 0000000..70b3995
--- /dev/null
@@ -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)
index a8cc127683a8a7ae9cb99f00668062a6a6e55c4f..9062cf6f5c3cf7e8c2dd47fbac278dc4035400a2 100644 (file)
@@ -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)
index 43b55b42a7d3bf18f8332321d256be52b713d414..9dfaf64ca5afd1397c83129abc9c536127809dfa 100644 (file)
@@ -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 {}
index 571b14ba728ffdbcc113a2a286cb513e7a48e584..728a76cdee19e10aa9ed264e1f7a0e23e8eb3c4c 100644 (file)
@@ -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)