[php-src] Issue #21392: missing overload for operators
[email protected] (jobs-git)
| Newsgroups | php.bugs |
|---|---|
| Message-ID | <[email protected]> |
Issue: https://github.com/php/php-src/issues/21392
Author: jobs-git
### Description
### Description
PHP does not have operator overload
### USE CASES
- **Readability and ease of use**
instead of writing it as:
```php
$a->add($b, $c, $d->div($s), $d->sub($r)->div($l->pow($p)))->sub($e->pow(10));
```
if the code has lots of this, user might be questioning why not use another tool, which itself introduces additional point of failures when mixed with backend php
with overload:
```php
$a + $b + $c + $d/$s - $e ** 10 + ($d - $r) / ($l ** $p)
```
its easier to understand and user can spot if there is a problem with the expression as defined
- **Operating on vectors**
as this demonstrates: https://gist.github.com/jbtronics/ee6431e52c161ddd006f8bb7e4f5bcd6
- **Matrix operations for Machine Learning, AI, Scientific calculations**
i think the current limitations of PHP is another factor slowing its adoption on these areas, while there are existing libraries that can do the heavy lifting, at some point user might need to build a backend code to process those results, possible solution might include 3rd party extensions or just use another tool, now PHP is behind, but there might be room for say WebML, Web AI or others?
- **Operating on complex backend data**
say business data that has multiple parameters in it:
```bash
$params ~ $base, $fee, $surge, $var1, $var2, $var3, $var4
```
consider simple operations like addition without object overload:
```php
$result_base = $base_b + $base_a
$result_fee = $fee_b + $fee_a
$result_surge = $surge_b + $surge_a
$result_var1 = $var1_b + $var1_a
$result_var2 = $var2_b + $var2_a
$result_var3 = $var3_b + $var3_a
$result_var4 = $var4_b + $var4_a
```
with overload, we can represent these as object and do this:
```php
$result = $a + $b
```
done
it still looks manageable with just two operand, but it becomes a nightmare with 3, would be almost unimaginable with this:
```php
$a + $b + $c + $d/$s - $e ** 10 + ($d - $r) / ($l ** $p)
```
and almost impossible for nested operations
### PHP Version
```plain
PHP 8.4.18 (cli) (built: Feb 13 2026 16:00:19) (NTS)
Copyright (c) The PHP Group
Built by Debian
Zend Engine v4.4.18, Copyright (c) Zend Technologies
with Zend OPcache v8.4.18, Copyright (c), by Zend Technologies
```
### Operating System
_No response_