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); } } class TaxFreePrice extends Price { protected function getRawValue() { return $this->value; } public function add(Price $other) { // Calls a protected method of whichever child class is $other $this->value += $other->getRawValue(); } } class PriceWithTax extends Price{ 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 git.master, git.master_jit, rfc.property-hooks
10.00 12.00 22.00

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
60.78 ms | 401 KiB | 8 Q