3v4l.org

run code in 500+ PHP versions simultaneously
<?php declare(strict_types=1); class ScoreAnalyzer { /** * @throws InvalidArgumentException */ public function __construct(private readonly int $score, private readonly int $number){ if(!is_int($score)){ throw new InvalidArgumentException('Score should be an Integer number'); } if(!is_int($number)){ throw new InvalidArgumentException('Number should be an Integer number'); } if($score < 0 || $score > 100){ throw new InvalidArgumentException('Score should be between 0 and 100'); } } public function analyzeScore(): void { echo "Score: $this->score \n"; $status = $this->determineStatus(); echo "Status: $status \n"; $grade = $this->calculateGrade(); echo "Grade: $grade\n"; echo "Number: $this->number\n"; $type = $this->findType(); echo "Type: $type\n"; } private function determineStatus(): string { return $this->score >= 60 ? "pass" : "fail"; } private function calculateGrade(): string { return match (true) { $this->score < 60 => 'F', $this->score < 70 => 'D', $this->score < 80 => 'C', $this->score < 90 => 'B', default => 'A', }; } private function findType(): string { return $this->number % 2 === 0 ? "even" : "odd"; } } try{ $scoreAnalyzer = new ScoreAnalyzer("t", 4); $scoreAnalyzer->analyzeScore(); } catch (Exception $e){ echo $e->getMessage(); }
Output for 8.2.31 - 8.2.33, 8.3.0 - 8.3.33, 8.4.1 - 8.4.24, 8.5.0 - 8.5.9
Fatal error: Uncaught TypeError: ScoreAnalyzer::__construct(): Argument #1 ($score) must be of type int, string given, called in /in/rb6Z4 on line 64 and defined in /in/rb6Z4:9 Stack trace: #0 /in/rb6Z4(64): ScoreAnalyzer->__construct('t', 4) #1 {main} thrown in /in/rb6Z4 on line 9
Process exited with code 255.

preferences:
63.09 ms | 792 KiB | 4 Q