3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Decimal { private string $value; public function __construct(string $value) { $this->value = $value; } public function toFixed(int $scale): string { // rough example for adjusting precision - do not use return bcadd($this->value, '0', $scale); } } class Entity { #[ORM\Column(type: 'decimal', precision: 10, scale: 4)] private string $subtotal = '0.0'; // name as desired public function getSubtotalDecimal(): Decimal { return new Decimal($this->subtotal, 4); } public function getSubtotal(): string { return $this->subtotal; } // see Union Types public function setSubtotal(string|Decimal $subtotal): self { if ($subtotal instanceof Decimal) { $subtotal = $subtotal->toFixed(4); } $this->subtotal = $subtotal; return $this; } } $decimal = new Decimal("1337.987654321"); $entity = new Entity(); $entity->setSubtotal($decimal); echo $entity->getSubtotal() . \PHP_EOL; echo $entity->getSubtotalDecimal()->toFixed(4) . \PHP_EOL;
Output for 8.0.17, 8.1.23 - 8.1.33, 8.2.10 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
1337.9876 1337.9876
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
106.73 ms | 407 KiB | 5 Q