Bug #80659 [Com]: unexp. Typecast at not fitting function return type declaration

From: Date: Fri, 22 Jan 2021 10:57:04 +0000
Subject: Bug #80659 [Com]: unexp. Typecast at not fitting function return type declaration
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-231703@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 Comment by: 3g3g3g4 at egeg3g dot com Reported by: thomas dot bindewald at metanet dot ch Summary: unexp. Typecast at not fitting function return type declaration 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: there is nothing to be discussed - stdClass is not compatible and when you want to allow any class simply use "object" no matter what, you expected a type error but didn't tell php that you expect strict mode Previous Comments: ------------------------------------------------------------------------ [2021-01-22 10:26:34] nikic@php.net 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). ------------------------------------------------------------------------ [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

« previous php.bugs (#231703) next »