3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class Price { protected $value; public function __construct($value) { $this->value = $value; } // Duck typing means this declaration is actually optional abstract protected function getRawValue(); public function getFormattedValue() { // Calls a protected method of whichever child class is $this return number_format($this->getRawValue(), 2); } public function add(Price $other) { // Calls a protected method of whichever child class is $other $this->value += $other->getRawValue(); } } class TaxFreePrice extends Price { protected function getRawValue() { return $this->value; } } class PriceWithTax extends Price{ private const TAX_RATE=1.2; protected function getRawValue() { return $this->value * self::TAX_RATE; } } $a = new TaxFreePrice(10); $b = new PriceWithTax(10); echo $a->getFormattedValue(), PHP_EOL; echo $b->getFormattedValue(), PHP_EOL; // Here, an instance of TaxFreePrice will access a protected member of PriceWithTax $a->add($b); echo $a->getFormattedValue(), PHP_EOL;
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
10.00 12.00 22.00
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 10.00 12.00 22.00

preferences:
163.19 ms | 402 KiB | 190 Q