Overriding GMP objects

From: Date: Wed, 26 Jun 2024 17:24:03 +0000
Subject: Overriding GMP objects
Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
Hello internals,

I've had this little library for a while (https://github.com/withinboredom/time), mostly as a
place to experiment with the quirks of PHP and take it in weird places. Anyway, I've been
experimenting with "strongly typed time" in attributes for a while now. At this point,
it's a little weird but doable to use something like this:

#[MyAttribute(units: StandardSecond, amount: 5)]

... which is less than ideal. What I would really like is this:

#[MyAttribute(5 * StandardSecond)]

PHP doesn't support operator overloading, which, from the last RFC attempt, probably won't
for quite a long time. Thus, I started experimenting with extending the \GMP class, which is
perfectly allowed since it isn't final. For those that don't know, GMP implements operator
overloading such that a GMP number times an int results in a new GMP number.

Surprisingly, I can get this to work, but I end up with a GMP number -- with the right value -- but
not the right type. Hence this email. In essence, I am attempting to "back-door" my way
into having operator overloading.

I would like to add static protected methods to the GMP class that are called for operations. In the
event all objects are base-GMP objects, no behavior will change.

So, here are my questions:

- for built-in extensions, does this need an RFC?
- does anyone have any strong opinions against having the ability to have actual, typed units in
PHP?
- should we revisit operator overloading instead of me hacking my way into it?
- should we revisit "constant expressions" in attributes?

— Rob


Thread (11 messages)

« previous php.internals (#123872) next »