]> granicus.if.org Git - php/commitdiff
Allow casting CurlHandle to int
authorNikita Popov <nikita.ppv@gmail.com>
Fri, 19 Jun 2020 14:50:59 +0000 (16:50 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Fri, 19 Jun 2020 21:05:47 +0000 (23:05 +0200)
(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.

ext/curl/interface.c
ext/curl/tests/curl_int_cast.phpt [new file with mode: 0644]

index 85ac0f0b3078dcd9cc82f933916b5534321d2a8a..6957e9e73a6492fe2a5dbd4eeb6bb0d5bd7604e4 100644 (file)
@@ -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 (file)
index 0000000..3f31b24
--- /dev/null
@@ -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)