# how to add annotations to an extension function

- Thread: https://externals.io/message/116873
- Original archive: https://news-web.php.net/php.internals/116873
- Started by Brett McBride on 2022-01-12T10:28:08+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #116873 — Brett McBride — 2022-01-12T10:28:08+00:00

URL: https://externals.io/message/116873#116873

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

---

## #116874 — Remi Collet — 2022-01-12T12:48:36+00:00

URL: https://externals.io/message/116873#116874 · In reply to: #116873

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

> 
> #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
