3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php declare(strict_types = 1); namespace App { class Status { const ACTIVE = 1; const INACTIVE = 2; const SOLD = 3; private $value; private $labels = [ self::ACTIVE => 'Active', self::ACTIVE => 'Inactive', self::ACTIVE => 'Sold', ]; /** * Status constructor. * @param $value */ public function __construct(int $value) { if (in_array($value, $this->labels)) { throw new \InvalidArgumentException('Invalid status'); } $this->value = $value; } public function __toString() { return $this->labels[$this->value]; } } class Product { private $id; private $name; private $status; /** * Product constructor. * @param $id * @param $name * @param $status */ public function __construct(int $id, string $name, Status $status) { $this->id = $id; $this->name = $name; $this->status = $status; } } $status = new Status(Status::SOLD); var_dump(new Product(1, 'Chess board', $status)); echo "\n\n\n\n{$status}"; }
based on 3rIZV
Output for 7.1.0 - 7.2.0
object(App\Product)#2 (3) { ["id":"App\Product":private]=> int(1) ["name":"App\Product":private]=> string(11) "Chess board" ["status":"App\Product":private]=> object(App\Status)#1 (2) { ["value":"App\Status":private]=> int(3) ["labels":"App\Status":private]=> array(1) { [1]=> string(4) "Sold" } } } Notice: Undefined offset: 3 in /in/f8Flv on line 35 Recoverable fatal error: Method App\Status::__toString() must return a string value in /in/f8Flv on line 61
Process exited with code 255.
Output for 7.0.0 - 7.0.20
object(App\Product)#2 (3) { ["id":"App\Product":private]=> int(1) ["name":"App\Product":private]=> string(11) "Chess board" ["status":"App\Product":private]=> object(App\Status)#1 (2) { ["value":"App\Status":private]=> int(3) ["labels":"App\Status":private]=> array(1) { [1]=> string(4) "Sold" } } } Notice: Undefined offset: 3 in /in/f8Flv on line 35 Catchable fatal error: Method App\Status::__toString() must return a string value in /in/f8Flv on line 61
Process exited with code 255.
Output for 5.4.12 - 5.6.28
Warning: Unsupported declare 'strict_types' in /in/f8Flv on line 2 Catchable fatal error: Argument 1 passed to App\Status::__construct() must be an instance of App\int, integer given, called in /in/f8Flv on line 59 and defined in /in/f8Flv on line 24
Process exited with code 255.
Output for 5.3.22 - 5.3.29
Warning: Unsupported declare 'strict_types' in /in/f8Flv on line 2 Parse error: syntax error, unexpected '[' in /in/f8Flv on line 14
Process exited with code 255.