3v4l.org

run code in 300+ PHP versions simultaneously
<?php $xml = ' <content> <hello attribute="123"></hello> </content> '; class Parser { private $xml; public function __construct(SimpleXMLElement $xml) { $this->xml = $xml; } public function asString(): string { return $this->getValue(); } public function asInt(): int { return $this->getValue(); } public function asCastedInt(): int { return $this->asString(); } public function directConversion() { return (int)$this->getValue(); } private function getValue() { return $this->xml->hello['attribute']; } } $object = new Parser(new SimpleXMLElement($xml)); var_dump($object->asString()); var_dump($object->asCastedInt()); var_dump($object->directConversion()); var_dump($object->asInt());
Output for 8.0.0 - 8.0.13, 8.1rc1 - rc3
string(3) "123" int(123) int(123) Fatal error: Uncaught TypeError: Parser::asInt(): Return value must be of type int, SimpleXMLElement returned in /in/JnFCh:22 Stack trace: #0 /in/JnFCh(44): Parser->asInt() #1 {main} thrown in /in/JnFCh on line 22
Process exited with code 255.
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.26
string(3) "123" int(123) int(123) Fatal error: Uncaught TypeError: Return value of Parser::asInt() must be of the type int, object returned in /in/JnFCh:22 Stack trace: #0 /in/JnFCh(44): Parser->asInt() #1 {main} thrown in /in/JnFCh on line 22
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.6 - 7.2.33
string(3) "123" int(123) int(123) Fatal error: Uncaught TypeError: Return value of Parser::asInt() must be of the type integer, object returned in /in/JnFCh:22 Stack trace: #0 /in/JnFCh(44): Parser->asInt() #1 {main} thrown in /in/JnFCh on line 22
Process exited with code 255.

preferences:
38.24 ms | 463 KiB | 5 Q