From: Nikita Popov Date: Fri, 19 Jun 2020 14:50:59 +0000 (+0200) Subject: Allow casting CurlHandle to int X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1c4463c77aa0291d065d5177428bbe080ca4755a;p=php 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. --- diff --git a/ext/curl/interface.c b/ext/curl/interface.c index 85ac0f0b30..6957e9e73a 100644 --- a/ext/curl/interface.c +++ b/ext/curl/interface.c @@ -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); @@ -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); @@ -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) diff --git a/ext/curl/tests/curl_int_cast.phpt b/ext/curl/tests/curl_int_cast.phpt new file mode 100644 index 0000000000..3f31b24210 --- /dev/null +++ b/ext/curl/tests/curl_int_cast.phpt @@ -0,0 +1,20 @@ +--TEST-- +Casting CurlHandle to int returns object ID +--FILE-- + +--EXPECT-- +int(1) +int(2) +int(2)