Skip to content

Add get_resource_id() function #5427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

nikic
Copy link
Member

@nikic nikic commented Apr 21, 2020

This is a more obvious and type-safe form of (int) $resource.

@@ -1505,6 +1505,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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess resource-ids will be re-used across request boundaries? if so, should be mentioned somewhere

Copy link
Contributor

@TysonAndre TysonAndre Apr 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd agree it should be documented. Obviously, this RFC doesn't change the existing behavior, changing it here is definitely out of scope, and the same note also applies to (int)$resource.
(e.g. "currently, resource ids are unique for the lifetime of execution (e.g. an http request in a web server), but this may change, because it's limited to 2**32 on 32-bit platforms)

I guess part of the issue is that it's using an associative array from integers to strings, and that zend_hash_index_del doesn't reset the last array pointer. I guess the only place where this could matter is that long-running 32-bit applications that create a lot of resources would possibly be affected by this (only able to create 4 billion ids in a lifetime for sockets/files, unless I misunderstood how this works), but for 64-bit applications, it doesn't matter.

Alternately, I guess it'd be possible to maintain a "free list" like what objects do so that resource ids can be reused.

php > for ($i = 0; $i < 3; $i++) { $fp = fopen("test$i.out", "w"); echo (int)$fp, "\n"; fclose($fp); $fp = null; }
2
3
4
php > $x = ['a', 'b'];
php > unset($x[1]);
php > $x[] = 'c';
php > var_export($x);
array (
  0 => 'a',
  2 => 'c',
)
ZEND_API void ZEND_FASTCALL zend_list_free(zend_resource *res)
{
	ZEND_ASSERT(GC_REFCOUNT(res) == 0);
	zend_hash_index_del(&EG(regular_list), res->handle);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GrahamCampbell
Copy link
Contributor

@nikic Does this function also work on curl objects?

@nikic
Copy link
Member Author

nikic commented Jun 30, 2020

@GrahamCampbell No.

@GrahamCampbell
Copy link
Contributor

I think it maybe should, since people may be upgrading their code to use get_resource_id, even if it is not PHP 8 only, using the Symfony polyfill. If curl objects are not going to be supported, and resources are ultimately going to be removed, I question why this function was even added. Maybe it should be removed from PHP 8, actually?

@nikic
Copy link
Member Author

nikic commented Jun 30, 2020

I can definitely see the argument for dropping this again, given the progress on resource removal.

@TysonAndre
Copy link
Contributor

Some ideas:

  1. Could a new interface LegacyResource be added, and have get_resource_id accept resource|LegacyResource instead of just resource (and make curl handles implement LegacyResource)
  2. Could get_resource_id() be changed to accept resource|object, or be replaced with a new function that does (get_resource_or_object_id(), get_internal_handle_id(), etc)

jfcherng added a commit to jfcherng-sublime/ST-Official-Packages that referenced this pull request Jul 18, 2020
jfcherng added a commit to jfcherng-sublime/ST-Official-Packages that referenced this pull request Jul 21, 2020
wbond pushed a commit to sublimehq/Packages that referenced this pull request Jul 21, 2020
* [PHP] Add some built-in classes and constants

Classes:

ArgumentCountError
ArithmeticError
CompileError
DivisionByZeroError
Error
ParseError
Stringable
TypeError
ValueError
WeakMap
WeakReference

---

Constants:

FILTER_VALIDATE_BOOL

---

https://github.com/symfony/polyfill-php80
https://wiki.php.net/rfc/stringable
https://wiki.php.net/rfc/weak_maps
https://www.php.net/manual/en/reserved.exceptions.php
https://www.php.net/manual/en/spl.exceptions.php

* [PHP] Add support for union types

https://wiki.php.net/rfc/union_types_v2

* [PHP] Add support for "static" return type

https://wiki.php.net/rfc/static_return_type

* [PHP] Add core function: str_contains

https://wiki.php.net/rfc/str_contains

* [PHP] Add core function: get_debug_type

* [PHP] Add built-in class: DateTimeImmutable, DateTimeInterface

* [PHP] Add core function: fdiv

php/php-src#4769

* [PHP] Add built-in class: PhpToken

https://wiki.php.net/rfc/token_as_object

* [PHP] Add core function: str_starts_with, str_ends_with

https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions

* [PHP] Add support for short attributes ("@@")

https://wiki.php.net/rfc/attributes_v2
https://wiki.php.net/rfc/attribute_amendments
https://wiki.php.net/rfc/shorter_attribute_syntax

* [PHP] Add support for "mixed" type hinting

https://wiki.php.net/rfc/mixed_type_v2

* [PHP] Add support for constructor property promotion

https://wiki.php.net/rfc/constructor_promotion

* [PHP] Extract visibility modifier as a variable

* [PHP] Add missing constant: DNS_CAA

Fixes #991

* [PHP] Add support for "match" expression

https://wiki.php.net/rfc/match_expression_v2

* [PHP] Add snippet: fn ...

* [PHP] Add snippet: match ...

* [PHP] Add support for named arguments

https://wiki.php.net/rfc/named_params

* [PHP] Add support for nullsafe operator

https://wiki.php.net/rfc/nullsafe_operator

* [PHP] Add core function: get_resource_id

php/php-src#5427

* [PHP] Add built-in class: InternalIterator

php/php-src#5216

* [PHP] Syntax definition use "version: 2"

* [PHP] Fix punctuation scopes have no begin/end
jrfnl added a commit to PHPCompatibility/PHPCompatibility that referenced this pull request Aug 10, 2020
> Added get_resource_id($resource) function, which returns the same value as
(int) $resource. It provides the same functionality under a clearer API.

Refs:
* https://github.com/php/php-src/blob/c0172aa2bdb9ea223c8491bdb300995b93a857a0/UPGRADING#L719-L720
* php/php-src#5427
* php/php-src@a0abc26
mitranim pushed a commit to mitranim/Packages that referenced this pull request Mar 25, 2022
* [PHP] Add some built-in classes and constants

Classes:

ArgumentCountError
ArithmeticError
CompileError
DivisionByZeroError
Error
ParseError
Stringable
TypeError
ValueError
WeakMap
WeakReference

---

Constants:

FILTER_VALIDATE_BOOL

---

https://github.com/symfony/polyfill-php80
https://wiki.php.net/rfc/stringable
https://wiki.php.net/rfc/weak_maps
https://www.php.net/manual/en/reserved.exceptions.php
https://www.php.net/manual/en/spl.exceptions.php

* [PHP] Add support for union types

https://wiki.php.net/rfc/union_types_v2

* [PHP] Add support for "static" return type

https://wiki.php.net/rfc/static_return_type

* [PHP] Add core function: str_contains

https://wiki.php.net/rfc/str_contains

* [PHP] Add core function: get_debug_type

* [PHP] Add built-in class: DateTimeImmutable, DateTimeInterface

* [PHP] Add core function: fdiv

php/php-src#4769

* [PHP] Add built-in class: PhpToken

https://wiki.php.net/rfc/token_as_object

* [PHP] Add core function: str_starts_with, str_ends_with

https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions

* [PHP] Add support for short attributes ("@@")

https://wiki.php.net/rfc/attributes_v2
https://wiki.php.net/rfc/attribute_amendments
https://wiki.php.net/rfc/shorter_attribute_syntax

* [PHP] Add support for "mixed" type hinting

https://wiki.php.net/rfc/mixed_type_v2

* [PHP] Add support for constructor property promotion

https://wiki.php.net/rfc/constructor_promotion

* [PHP] Extract visibility modifier as a variable

* [PHP] Add missing constant: DNS_CAA

Fixes sublimehq#991

* [PHP] Add support for "match" expression

https://wiki.php.net/rfc/match_expression_v2

* [PHP] Add snippet: fn ...

* [PHP] Add snippet: match ...

* [PHP] Add support for named arguments

https://wiki.php.net/rfc/named_params

* [PHP] Add support for nullsafe operator

https://wiki.php.net/rfc/nullsafe_operator

* [PHP] Add core function: get_resource_id

php/php-src#5427

* [PHP] Add built-in class: InternalIterator

php/php-src#5216

* [PHP] Syntax definition use "version: 2"

* [PHP] Fix punctuation scopes have no begin/end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants