3v4l.org

run code in 300+ PHP versions simultaneously
<?php class StringNotEmptyType { public string $s; public function __construct(string $s) { $s = trim($s); if ($s === '') { throw new Exception('String must not be emtpy'); } $this->s = $s; } public function __toString() : string { return $this->s; } public static function create(string $s) : self { return new self($s); } } class Something { public StringNotEmptyType $s; } $okay = new Something(); $okay->s = StringNotEmptyType::create('not empty'); echo $okay->s; $okay = new Something(); $okay->s = StringNotEmptyType::create('');

preferences:
53.47 ms | 402 KiB | 5 Q