3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ShopProduct { public $discount = 0; public $title; public $producerMainName; public $producerFirstName; protected $price; function __construct($title, $firstName, $mainName, $price) { $this->title = $title; $this->producerFirstName = $firstName; $this->producerMainName = $mainName; $this->price = $price; } function getSummaryLine(){ $base = "{$this->title} ( {$this->producerMainName}, "; $base .= "{$this->producerFirstName} )"; return $base; } function getProducer() { return "{$this->producerFirstName} " . "{$this->producerMainName}"; } function setDiscount(){ $this->discount=$num; } public function getPrice() { # code... return ($this->price - $this->discount); } } class ShopProductWriter{ private $products = array(); public function addProduct( ShopProduct $shopProduct){ $this->product[] = $shopProduct; } public function write(){ $str = ""; foreach ($this->products as $shopProduct) { $str .= "{$shopProduct->title}: "; $str .= "{$shopProduct->getProducer()}"; $str .= "{$shopProduct->getPrice()}\n"; } print $str; } } class CDProduct extends ShopProduct { public $playLength; function __construct($title, $firstName, $mainName, $price, $playLength){ parent::__construct($title, $firstName, $mainName, $price); $this->playLength = $playLength; } function getPlayLength(){ return $this->playLength; } function getSummaryLine(){ $base = parent::getSummaryLine(); $base .= " :Legth of song {$this->playLength}"; return $base; } } class BookProduct extends shopProduct{ public $numPages; function __construct($title, $firstName, $mainName, $price, $numPages){ parent::__construct($title, $firstName, $mainName, $price); $this->playLength = $playLength; } function getSummaryLine(){ $base = parent::getSummaryLine(); $base .= ": Number of pages - {$this->numPages}"; } function getProducer() { return "{$this->producerFirstName} " . "{$this->producerMainName}"; } function getPrice(){ return $this->price; } } $product2 = new CDProduct("On my own", "Ashes", "Remain", 10.99, 2.53); $product3 = new CDProduct("On my own", "Ashes", "Remain", 10.99, 2.53); $product4 = new CDProduct("On my own", "Ashes", "Remain", 10.99, 2.53); $product5 = new CDProduct("On my own", "Ashes", "Remain", 10.99, 2.53); print ShopProductWriter::write();
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught Error: Non-static method ShopProductWriter::write() cannot be called statically in /in/InQJ6:110 Stack trace: #0 {main} thrown in /in/InQJ6 on line 110
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.31, 7.4.0 - 7.4.33
Deprecated: Non-static method ShopProductWriter::write() should not be called statically in /in/InQJ6 on line 110 Fatal error: Uncaught Error: Using $this when not in object context in /in/InQJ6:51 Stack trace: #0 /in/InQJ6(110): ShopProductWriter::write() #1 {main} thrown in /in/InQJ6 on line 51
Process exited with code 255.
Output for 7.3.32 - 7.3.33
Fatal error: Uncaught Error: Using $this when not in object context in /in/InQJ6:51 Stack trace: #0 /in/InQJ6(110): ShopProductWriter::write() #1 {main} thrown in /in/InQJ6 on line 51
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.21
Strict Standards: Non-static method ShopProductWriter::write() should not be called statically in /in/InQJ6 on line 110 Fatal error: Using $this when not in object context in /in/InQJ6 on line 51
Process exited with code 255.

preferences:
136.8 ms | 402 KiB | 182 Q