Skip to content

Allow casting CurlHandle to int #5743

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ext/curl/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ static void curl_free_obj(zend_object *object);
static HashTable *curl_get_gc(zend_object *object, zval **table, int *n);
static zend_function *curl_get_constructor(zend_object *object);
static zend_object *curl_clone_obj(zend_object *object);
static int curl_cast_object(zend_object *obj, zval *result, int type);
php_curl *init_curl_handle_into_zval(zval *curl);
static inline int build_mime_structure_from_hash(php_curl *ch, zval *zpostfields);

Expand Down Expand Up @@ -1204,6 +1205,7 @@ PHP_MINIT_FUNCTION(curl)
curl_object_handlers.get_gc = curl_get_gc;
curl_object_handlers.get_constructor = curl_get_constructor;
curl_object_handlers.clone_obj = curl_clone_obj;
curl_object_handlers.cast_object = curl_cast_object;

curl_multi_register_class(class_CurlMultiHandle_methods);
curl_share_register_class(class_CurlShareHandle_methods);
Expand Down Expand Up @@ -1303,6 +1305,18 @@ static HashTable *curl_get_gc(zend_object *object, zval **table, int *n)
return zend_std_get_properties(object);
}

static int curl_cast_object(zend_object *obj, zval *result, int type)
{
if (type == IS_LONG) {
/* For better backward compatibility, make (int) $curl_handle return the object ID,
* similar to how it previously returned the resource ID. */
ZVAL_LONG(result, obj->handle);
return SUCCESS;
}

return zend_std_cast_object_tostring(obj, result, type);
}

/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(curl)
Expand Down
20 changes: 20 additions & 0 deletions ext/curl/tests/curl_int_cast.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Casting CurlHandle to int returns object ID
--FILE--
<?php

$handle1 = curl_init();
var_dump((int) $handle1);
$handle2 = curl_init();
var_dump((int) $handle2);

// NB: Unlike resource IDs, object IDs are reused.
unset($handle2);
$handle3 = curl_init();
var_dump((int) $handle3);

?>
--EXPECT--
int(1)
int(2)
int(2)