how to add annotations to an extension function

php.internals

Brett McBride

4 years ago
Hello, I'm pretty stuck, trying to make an existing PHP extension (google/protobuf) compatible with php 8.1 [1] whilst retaining backwards-compatibility (ideally back to 7.0, but that might be negotiable). I'm not a C programmer, and definitely not a PHP extension developer. Some methods in the extension implement Iterator and ArrayAccess, and with 8.1 show deprecation notices, eg: Deprecated: Return type of Google\Protobuf\Internal\RepeatedField::offsetGet($index) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in Unknown on line 0 Given that the mixed type is new, I know I can't add it directly as a return type without breaking BC, so my next thought was that I could try to find a method that can create an annotation, and conditionally add it. On php-general, I was pointed at the "zend_add_attribute" method, and I am hopeful that something like this might work, if I knew how to call the method: #if PHP_VERSION >= 80100 zend_add_attribute("returntypewillchange") #endif Failing that, I'm after any guidance on whether anybody thinks this can be done, or a pointer to some prior art I can borrow heavily from would be amazing! Thanks in advance. [1] <https://github.com/protocolbuffers/protobuf/pull/9370>

Remi Collet

4 years ago
Le 12/01/2022 à 11:28, Brett McBride a écrit :
> Hello, > I'm pretty stuck, trying to make an existing PHP extension > (google/protobuf) compatible with php 8.1 [1] whilst retaining > backwards-compatibility (ideally back to 7.0, but that might be > negotiable). I'm not a C programmer, and definitely not a PHP extension > developer. > Some methods in the extension implement Iterator and ArrayAccess, and with > 8.1 show deprecation notices, eg: > > Deprecated: Return type > of Google\Protobuf\Internal\RepeatedField::offsetGet($index) should either > be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the > #[\ReturnTypeWillChange] attribute should be used to temporarily > suppress the notice in Unknown on line 0 > > Given that the mixed type is new, I know I can't add it directly as a > return type without breaking BC, so my next thought was that I could try to > find a method that can create an annotation, and conditionally add it. On > php-general, I was pointed at the "zend_add_attribute" method, and I am > hopeful that something like this might work, if I knew how to call the > method:
See the ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_* macros used to declare the method arginfo Ex: https://github.com/php-kafka/php-simple-kafka-client/blob/main/metadata_collection.stub.php used to generate https://github.com/php-kafka/php-simple-kafka-client/blob/main/metadata_collection_arginfo.h And with compatibility hack for older version https://github.com/php-kafka/php-simple-kafka-client/blob/main/metadata_collection.c#L44 Cheers, Remi