Skip to content

Commit 1c4463c

Browse files
committed
Allow casting CurlHandle to int
(int) $curlHandle will return spl_object_id($curlHandle). This makes curl handle objects backwards compatible with code using (int) $curlHandle to obtain a resource ID. Closes GH-5743.
1 parent 7e2147b commit 1c4463c

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

ext/curl/interface.c

+14
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ static void curl_free_obj(zend_object *object);
241241
static HashTable *curl_get_gc(zend_object *object, zval **table, int *n);
242242
static zend_function *curl_get_constructor(zend_object *object);
243243
static zend_object *curl_clone_obj(zend_object *object);
244+
static int curl_cast_object(zend_object *obj, zval *result, int type);
244245
php_curl *init_curl_handle_into_zval(zval *curl);
245246
static inline int build_mime_structure_from_hash(php_curl *ch, zval *zpostfields);
246247

@@ -1204,6 +1205,7 @@ PHP_MINIT_FUNCTION(curl)
12041205
curl_object_handlers.get_gc = curl_get_gc;
12051206
curl_object_handlers.get_constructor = curl_get_constructor;
12061207
curl_object_handlers.clone_obj = curl_clone_obj;
1208+
curl_object_handlers.cast_object = curl_cast_object;
12071209

12081210
curl_multi_register_class(class_CurlMultiHandle_methods);
12091211
curl_share_register_class(class_CurlShareHandle_methods);
@@ -1303,6 +1305,18 @@ static HashTable *curl_get_gc(zend_object *object, zval **table, int *n)
13031305
return zend_std_get_properties(object);
13041306
}
13051307

1308+
static int curl_cast_object(zend_object *obj, zval *result, int type)
1309+
{
1310+
if (type == IS_LONG) {
1311+
/* For better backward compatibility, make (int) $curl_handle return the object ID,
1312+
* similar to how it previously returned the resource ID. */
1313+
ZVAL_LONG(result, obj->handle);
1314+
return SUCCESS;
1315+
}
1316+
1317+
return zend_std_cast_object_tostring(obj, result, type);
1318+
}
1319+
13061320
/* {{{ PHP_MSHUTDOWN_FUNCTION
13071321
*/
13081322
PHP_MSHUTDOWN_FUNCTION(curl)

ext/curl/tests/curl_int_cast.phpt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Casting CurlHandle to int returns object ID
3+
--FILE--
4+
<?php
5+
6+
$handle1 = curl_init();
7+
var_dump((int) $handle1);
8+
$handle2 = curl_init();
9+
var_dump((int) $handle2);
10+
11+
// NB: Unlike resource IDs, object IDs are reused.
12+
unset($handle2);
13+
$handle3 = curl_init();
14+
var_dump((int) $handle3);
15+
16+
?>
17+
--EXPECT--
18+
int(1)
19+
int(2)
20+
int(2)

0 commit comments

Comments
 (0)