Make the GMP class final View as Markdown

Implemented (PHP 8.4)
Authors
Gina Peter Banyard girgias@php.net
Date
2024-06-29
Version
0.1
Target
PHP 8.4
Implementation
https://github.com/php/php-src/pull/15121
Source
wiki.php.net/rfc/gmp-final

Vote

Accept Make the GMP class final RFC? closed
Yes
28
No
0
28 voters
asgrim Yes 2024-07-15 08:41
brzuchal Yes 2024-07-14 04:43
bwoebi Yes 2024-07-20 22:19
crell Yes 2024-07-13 15:23
derick Yes 2024-07-15 15:59
devnexen Yes 2024-07-13 15:42
ericmann Yes 2024-07-15 16:09
galvao Yes 2024-07-19 12:57
girgias Yes 2024-07-13 15:04
jimw Yes 2024-07-13 17:38
josh Yes 2024-07-25 16:34
kguest Yes 2024-07-17 10:41
kocsismate Yes 2024-07-15 08:47
levim Yes 2024-07-15 14:53
mauricio Yes 2024-07-14 07:30
mbeccati Yes 2024-07-15 12:14
nielsdos Yes 2024-07-13 18:19
ocramius Yes 2024-07-13 16:15
petk Yes 2024-07-13 20:36
pierrick Yes 2024-07-16 13:24
ramsey Yes 2024-07-13 15:13
rasmus Yes 2024-07-17 00:49
saki Yes 2024-07-13 16:07
sebastian Yes 2024-07-16 06:28
sergey Yes 2024-07-15 10:05
timwolla Yes 2024-07-13 15:42
trowski Yes 2024-07-16 22:47
weierophinney Yes 2024-07-23 12:58

Discussions on the mailing list

Convert ext/xml to use an object instead of resource
by Nikita Popov · 13 messages
What to do with opaque GMP objects which allows instantiation
by G. P. B. · 4 messages
[RFC] Make the GMP class final
by Gina P. Banyard · 12 messages
[RFC] [VOTE] Make the GMP class final
by Gina P. Banyard · 2 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:50 UTC

Introduction

The GMP class was introduced as part of the Internal operator overloading and GMP improvementsRFC.

This RFC also converted the GMP extension to use an object instead of a resource. One of the first proposals of its kind. Since then numerous resources have been converted to what we call opaque objects. [1][2][3] And the finalization of the process was codified in the Resource to object conversionRFC which was accepted in late 2023.

One element of the resource to object migration is to make the object opaque, which in general implied, being final, being not serializable, not constructible via new, cannot be cast, and to not implement any methods.

The GMP class is a notable exception to many of the above restrictions. It is a non-final class, that can be instantiated via new, and is serializable. The serialization aspect was intended as part of the initial RFC, but the other aspects were not. Indeed, the GMP class did not have a proper constructor to initialize the object until PHP 8.2.4 after this fact was discovered and discussed on the mailing list in late December 2022. [4]

As such, we deem the fact that GMP was not made final an oversight and want to correct this.

Proposal

Make the GMP class final.

Backward Incompatible Changes

Any userland class that extends GMP would now throw a compile time error.

From an analysis of private and publicly available code bases with Exakatno one has ever extended this class, as such the BC Break is mostly theoretical.

Further Motivation

In the discussion of the new BCMath\Number class introduced via the Support object type in BCMathRFC, it was explicitly discussed if the class should be final or not. And the consensus was that it should be final due to concerns about overloading the methods.

However, people seemingly want to “enhance” GMP to achieve userland operator overloading by hooking into it's logic when extending it. This is in total contradiction with the above RFC discussion which happened a few months ago. This resembles more of a “hack” than anything else and support for a proper form of operator overloading [5][6] should be revisited instead. Another alternative would be to create a dedicated PHP extension with an object that overloads the do_operator object handler.

Therefore, we think it is crucial to close this avenue for discussion once and for all.

Version

Next minor version, which is PHP 8.4.

Vote

As per the voting RFC a yes/no vote with a 2/3 majority is needed for this proposal to be accepted.

Voting started on 2024-07-13 and will end on 2024-07-27.

Vote: Accept Make the GMP class final RFC? — Yes: 28 · No: 0 (closed)

References

[1] “PHP RFC: Migration Hash Context from Resource to Object”: https://externals.io/rfc/hash-context.as-resource

[2] “Convert ext/xml to use an object instead of resource” PHP Internals Mailing List thread: https://externals.io/message/104361

[3] “Resource to opaque object migration” php/php-tasks issue: https://github.com/php/php-tasks/issues/6

[4] “What to do with opaque GMP objects which allows instantiation” PHP Internals Mailing List thread: https://externals.io/message/119216

[5] “PHP RFC: Userspace operator overloading”: https://wiki.php.net/rfc/userspace_operator_overloading

[6] “PHP RFC: User Defined Operator Overloads”: https://wiki.php.net/rfc/user_defined_operator_overloads