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.
This commit is contained in:
Nikita Popov 2020-04-21 12:31:17 +02:00
parent d5d99ce8d1
commit a0abc26ef7
5 changed files with 44 additions and 0 deletions

View file

@ -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

View file

@ -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)

View 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)

View 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 {}

View 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)