3v4l.org

run code in 300+ PHP versions simultaneously
<?php class BankTransfer {} class CreditCard {} class Payment { private $creditCard; private $banTransfer; private function __construct(BankTransfer $bankTransfer = null, CreditCard $creditCard = null) { $this->bankTransfer = $bankTransfer; $this->creditCard = $creditCard; } public static function byCreditCard(CreditCard $creditCard) { $ccPayment = new self(); $ccPayment->creditCard = $creditCard; return $ccPayment; } public static function byBankTransfer(BankTransfer $bankTransfer) { return new self($bankTransfer, null); } public function isCreditCard() { return isset($this->creditCard); } public function isBankTransfer() { return isset($this->bankTransfer); } public function getBankTransfer() { if ($this->isBankTransfer()) { return $this->bankTransfer; } throw new \Exception(); } public function getCreditCard() { if ($this->isCreditCard()) { return $this->creditCard; } throw new \Exception(); } } class Booking { private $payment; public function __construct(Payment $payment) { $this->payment = $payment; } public function getPayment() { return $this->payment; } } //$payment = Payment::byBankTransfer(new BankTransfer()); $payment = Payment::byCreditCard(new CreditCard()); $booking = new Booking($payment); if ($booking->getPayment()->isCreditCard()) { echo 'credit card'; } if ($booking->getPayment()->isBankTransfer()) { echo 'bank transfer'; }
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: Creation of dynamic property Payment::$bankTransfer is deprecated in /in/iDq7G on line 12 credit card

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:
83.65 ms | 401 KiB | 8 Q