3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Car { protected Supplier $supplier; public function __construct() { $this->supplier = new Supplier(); } final public function getSupplier(): Supplier { return $this->supplier; } } class Supplier { const SUPPLIERS = [ 1 => 'toyota', 2 => 'bmw' ]; private $id; private $name; final public function set(mixed $value): void { if (is_int($value)) { $this->id = $value; $this->name = self::SUPPLIERS[$value]; } elseif (is_string($value)) { $this->name = $value; $this->id = array_search($value, self::SUPPLIERS); } else { throw new \InvalidArgumentException('Invalid value:' . $value); } } public function setByName(string $name) { $this->name = $name; $this->id = array_search($name, self::SUPPLIERS); } public function getId(): int { return $this->id; } public function getName(): string { return $this->name; } } $car = new Car(); // IdのセットでNameも自動でセットされる $car->getSupplier()->set(1); echo $car->getSupplier()->getId() . PHP_EOL; // 1 echo $car->getSupplier()->getName() . PHP_EOL; // toyota // NameのセットでIdも自動でセットされる $car->getSupplier()->set('bmw'); echo $car->getSupplier()->getId() . PHP_EOL; // 2 echo $car->getSupplier()->getName() . PHP_EOL; // bmw
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.4.16, 8.5.0 - 8.5.1
1 toyota 2 bmw
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:
108.43 ms | 407 KiB | 5 Q