Bug #80659 [Opn->Nab]: unexp. Typecast at not fitting function return type declaration
| From: | nikic@php.net | Date: | Fri, 22 Jan 2021 10:26:34 +0000 |
| Subject: | Bug #80659 [Opn->Nab]: unexp. Typecast at not fitting function return type declaration | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-231702@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80659&edit=1
ID: 80659
Updated by: nikic@php.net
Reported by: thomas dot bindewald at metanet dot ch
Summary: unexp. Typecast at not fitting function return type
declaration
-Status: Open
+Status: Not a bug
Type: Bug
Package: Class/Object related
Operating System: Linux 4.19.0-13-amd64 x86_64 GNU
PHP Version: 8.0.1
Block user comment: N
Private report: N
New Comment:
This is correct, because your return type includes "string" and SimpleXMLElement can be
converted to string.
If you don't want implicit conversions, use declare(strict_types=1).
Previous Comments:
------------------------------------------------------------------------
[2021-01-22 10:24:35] thomas dot bindewald at metanet dot ch
There could be discussed, if returning the SimpleXMLElement would be okay, as the return type
declaration allows "stdClass" (?).
But a silent "empty string"-conversion/casting is definitely unexpected.
------------------------------------------------------------------------
[2021-01-22 09:54:06] thomas dot bindewald at metanet dot ch
Description:
------------
Environment: Debian 10 Linux
PHP-Packages from https://packages.sury.org/php/
---
PHP 8.0.1 (cli) (built: Jan 12 2021 13:59:46) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.1, Copyright (c) Zend Technologies
with Zend OPcache v8.0.1, Copyright (c), by Zend Technologies
with Xdebug v3.0.1, Copyright (c) 2002-2020, by Derick Rethans
Test script:
---------------
<?php
âfunction foooo() : false|string|stdClass {
$xmlString =
'<xml><woche><Montag>Erdbeermarmelade</Montag><Dienstag>Lindenbluetenhonig</Dienstag></woche></xml>';
$xmlObj = new SimpleXMLElement($xmlString);
var_dump($xmlObj);
return $xmlObj;
}
â$wert = foooo();
var_dump($wert);
exit;
Expected result:
----------------
TypeError Exception being thrown.
Actual result:
--------------
XML-Object silently being casted to empty string.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80659&edit=1