3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ValueObject implements JsonSerializable { private $value; public function __construct($value) { $this->value = $value; } public function jsonSerialize() { return $this->value; } } class Age extends ValueObject { public function __construct(int $value) { if ($value > 150) { throw new Exception('Age must be less than 150.'); } parent::__construct($value); } } function Email(string $email) { class Email extends ValueObject { public function __construct(string $value) { $email = filter_var($value, FILTER_VALIDATE_EMAIL); if (false === $email) { throw new Exception('Email is invalid.'); } parent::__construct($email); } } return new Email($email); } function Age(int $age) { return new Age($age); } // class User implements JsonSerializable { // private $email; // private $age; // public function __construct(Email $email) { // $this->email = $email; // } // public function setAge(Age $age) { $this->age = $age; } // public function jsonSerialize() { // return ['email' => $this->email, 'age' => $this->age]; // } // public function __destruct() // { // // persist // } // } // class UpdateUserCommand { // private $userId; // private $email; // private $age; // private $location; // public function __construct(GUID $userId, Email $email, Age $age = null, Point $location = null) { // $this->userId = $userId; // $this->email = $email; // $this->age = $age; // $this->location = $location; // } // public function getUserId() { return $this->userId; } // public function getEmail() { return $this->email; } // public function getAge() { return $this->age; } // public function getLocation() { return $this->location; } // } // class UserService { // private $users; // public function __construct(Users $users) { // $this->users = $users; // } // public function update(UpdateUserCommand $command) { // $user = $this->users[$command->getUserId()]; // $user->setEmail($command->getEmail()); // $user->setAge($command->getAge()); // $user->setPoint($command->getPoint()); // } // } $email = Email('opexus@gmail.com'); $age = Age(23); // $lattitude = new Lattitude(-20); // $longitude = new Longitude(170); // $point = new Point($lattitude, $longitude); // $user = new User($email); // $user->setAge($age); echo json_encode(compact('email', 'age'));
Output for 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Deprecated: Return type of ValueObject::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/bUpfS on line 7 {"email":"opexus@gmail.com","age":23}
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
{"email":"opexus@gmail.com","age":23}
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.28
Catchable fatal error: Argument 1 passed to Email() must be an instance of string, string given, called in /in/bUpfS on line 82 and defined in /in/bUpfS on line 20
Process exited with code 255.

preferences:
188.1 ms | 402 KiB | 234 Q