<?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());
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 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
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.33
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.
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.