3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class StringObject { protected string $string; public function __tostring(): string { return $this->string; } } class StringObjectException extends Exception {} class CountableString extends StringObject { private int $lettersCount; public function __construct(string $string) { $this->string = $string; $this->lettersCount = mb_strlen($string); } public function countLetters(): int { return $this->lettersCount; } } class EmptyStringException extends Exception {}; class NotEmptyString extends StringObject { public function __construct(string $string) { if ('' === $string) { throw new EmptyStringException('Expected not empty string.'); } $this->string = $string; } } class TooLongStringException extends Exception {}; class TenLettersString extends StringObject { private const MAX_LETTERS_COUNT = 10; public function __construct(CountableString $string) { if (self::MAX_LETTERS_COUNT < $string->countLetters()) { throw new TooLongStringException( sprintf('Expected string not longer then: "%d letters" got: "%s"', self::MAX_LETTERS_COUNT, $string ) ); } $this->string = $string; } } $countableString = new CountableString('Тестовая строка'); $notEmptyString = new NotEmptyString($countableString); echo $countableString->countLetters() . PHP_EOL; echo $countableString . PHP_EOL; $tenLetterLongString = new TenLettersString($countableString);
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
15 Тестовая строка Fatal error: Uncaught TooLongStringException: Expected string not longer then: "10 letters" got: "Тестовая строка" in /in/XhAE2:45 Stack trace: #0 /in/XhAE2(63): TenLettersString->__construct(Object(CountableString)) #1 {main} thrown in /in/XhAE2 on line 45
Process exited with code 255.

preferences:
72 ms | 407 KiB | 5 Q