Bug #81036 [Com]: TypeError is raised instead of implizit type casting

From: Date: Wed, 12 May 2021 11:06:40 +0000
Subject: Bug #81036 [Com]: TypeError is raised instead of implizit type casting
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-233813@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81036&edit=1

 ID:                 81036
 Comment by:         bugs-php at daniel-siepmann dot de
 Reported by:        bugs-php at daniel-siepmann dot de
 Summary:            TypeError is raised instead of implizit type casting
 Status:             Not a bug
 Type:               Bug
 Package:            *Programming Data Structures
 Operating System:   Ubuntu
 PHP Version:        7.4.19
 Block user comment: N
 Private report:     N

 New Comment:

Thanks for the near instant response.

I fully understand that design. Still I'm lacking a proper info within docs. I would like to
figure out how to contribute there.

I guess the design is already documented via "if possible" at https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.strict
?
I would then try to improve that part.


Previous Comments:
------------------------------------------------------------------------
[2021-05-12 10:27:27] nikic@php.net

Explicit type casts like (int) perform a cast at all costs, even if it makes absolutely no sense --
you can cast "foobar" to int, you can cast [1, 2, 3] to int, you can cast new stdClass to
int.

Type declarations have much stricter rules, even if strict_types is not used. They only allow
conversions if they are "somewhat reasonable". In particular, you can implicitly convert
"10" to 10, but you can't implicitly convert "foobar" to 0. If you want to
do that, you need to use an explicit type cast.

This is by design.

------------------------------------------------------------------------
[2021-05-12 10:23:23] bugs-php at daniel-siepmann dot de

Description:
------------
Given a script without declare_strict but a method with defined return type.
The method returns an integer but the stored value might be a string.

I would expect PHP to do a type cast which works in general.
But it does not work with example string.

Test script:
---------------
class Test
{
    protected $property;
    public function __construct() {
        $this->property = 10;
    }
    public function getProperty(): ?int {
        return $this->property;
    }
    public function setProperty($value) {
        $this->property = $value;
    }
}
$subject = new Test;
var_dump($subject->getProperty()); // works
$subject->setProperty((int) '{$PID.event.import.ismf.storagePid}'); // works
var_dump($subject->getProperty()); // works
$subject->setProperty('{$PID.event.import.ismf.storagePid}'); // works
var_dump($subject->getProperty()); // breaks



Expected result:
----------------
I would expect the last line to return the integer 0 instead of an TypeError.

Actual result:
--------------
The script fails with

PHP Fatal error:  Uncaught TypeError: Return value of Test::getProperty() must be of the type int,
string returned


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=81036&edit=1


Thread (4 messages)

« previous php.bugs (#233813) next »