From 44df0abc02289ea8dfb6e060208e7f758b743cff Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Thu, 9 Sep 2021 21:29:28 -0700 Subject: [PATCH] Added support for PHP 8.1 (currently in RC1). --- php/ext/google/protobuf/array.c | 4 ++-- php/ext/google/protobuf/map.c | 4 ++-- php/ext/google/protobuf/protobuf.h | 12 +++++++++++- php/tests/GeneratedClassTest.php | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/php/ext/google/protobuf/array.c b/php/ext/google/protobuf/array.c index 765e90297c38..2c9a7108b9d4 100644 --- a/php/ext/google/protobuf/array.c +++ b/php/ext/google/protobuf/array.c @@ -640,8 +640,8 @@ void Array_ModuleInit() { repeated_field_methods); RepeatedField_class_entry = zend_register_internal_class(&tmp_ce); - zend_class_implements(RepeatedField_class_entry, 3, spl_ce_ArrayAccess, - zend_ce_aggregate, spl_ce_Countable); + zend_class_implements(RepeatedField_class_entry, 3, zend_ce_arrayaccess, + zend_ce_aggregate, zend_ce_countable); RepeatedField_class_entry->ce_flags |= ZEND_ACC_FINAL; RepeatedField_class_entry->create_object = RepeatedField_create; diff --git a/php/ext/google/protobuf/map.c b/php/ext/google/protobuf/map.c index f5890d99e4fd..bbdfe29c5d70 100644 --- a/php/ext/google/protobuf/map.c +++ b/php/ext/google/protobuf/map.c @@ -636,8 +636,8 @@ void Map_ModuleInit() { MapField_methods); MapField_class_entry = zend_register_internal_class(&tmp_ce); - zend_class_implements(MapField_class_entry, 3, spl_ce_ArrayAccess, - zend_ce_aggregate, spl_ce_Countable); + zend_class_implements(MapField_class_entry, 3, zend_ce_arrayaccess, + zend_ce_aggregate, zend_ce_countable); MapField_class_entry->ce_flags |= ZEND_ACC_FINAL; MapField_class_entry->create_object = MapField_create; diff --git a/php/ext/google/protobuf/protobuf.h b/php/ext/google/protobuf/protobuf.h index f00fd4302569..983b9b3ef69b 100644 --- a/php/ext/google/protobuf/protobuf.h +++ b/php/ext/google/protobuf/protobuf.h @@ -52,7 +52,7 @@ const zval *get_generated_pool(); #define PROTO_RETURN_VAL zval* #endif -// Sine php 8.0, the Object Handlers API was changed to receive zend_object* +// Since php 8.0, the Object Handlers API was changed to receive zend_object* // instead of zval* and zend_string* instead of zval* for property names. // https://github.com/php/php-src/blob/php-8.0.0beta1/UPGRADING.INTERNALS#L37-L39 #if PHP_VERSION_ID < 80000 @@ -74,6 +74,16 @@ const zval *get_generated_pool(); #define PROTO_STRLEN_P(obj) ZSTR_LEN(obj) #endif +// In PHP 8.1, several old interfaces are removed: +// https://github.com/php/php-src/blob/14f599ea7def7c7a59c40aff763ce8b105573e7a/UPGRADING.INTERNALS#L27-L31 +// +// We now use the new interfaces (zend_ce_arrayaccess and zend_ce_countable). +// However we have to polyfill zend_ce_countable, which was only introduced in +// PHP 7.2.0. +#if PHP_VERSION_ID < 70200 +#define zend_ce_countable spl_ce_Countable +#endif + ZEND_BEGIN_ARG_INFO(arginfo_void, 0) ZEND_END_ARG_INFO() diff --git a/php/tests/GeneratedClassTest.php b/php/tests/GeneratedClassTest.php index 2b15e42c817e..837f05215ddc 100644 --- a/php/tests/GeneratedClassTest.php +++ b/php/tests/GeneratedClassTest.php @@ -565,7 +565,7 @@ public function testMapFieldViaArray() $m->setMapInt32Int32($dict); $this->assertSame(0, count($m->getMapInt32Int32())); - $dict = array(5 => 5, 6.1 => 6.1, "7" => "7"); + $dict = array(5 => 5, 6 => 6.1, "7" => "7"); $m->setMapInt32Int32($dict); $this->assertTrue($m->getMapInt32Int32() instanceof MapField); $this->assertSame(3, count($m->getMapInt32Int32()));