php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80659 unexp. Typecast at not fitting function return type declaration
Submitted: 2021-01-22 09:54 UTC Modified: 2021-01-22 16:08 UTC
From: thomas dot bindewald at metanet dot ch Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 8.0.1 OS: Linux 4.19.0-13-amd64 x86_64 GNU
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: thomas dot bindewald at metanet dot ch
New email:
PHP Version: OS:

 

 [2021-01-22 09:54 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-01-22 10:24 UTC] 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 10:26 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2021-01-22 10:26 UTC] 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:57 UTC] 3g3g3g4 at egeg3g dot com
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
 [2021-01-22 16:08 UTC] thomas dot bindewald at metanet dot ch
after the first hint (thanks @ nikic) I was able to research the topic deeper and found the relevant aspects. I was no longer aware of the details.
Thanks & I apologize for my bringing up this issue.


/close
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC