php-src icon indicating copy to clipboard operation
php-src copied to clipboard

`ReflectionProperty::setValue()` does implicit conversion using strict types

Open franmomu opened this issue 4 years ago • 2 comments

Description

The following code:

<?php

declare(strict_types=1);

class Test
{
    public string $property = '';
}

$test = new Test();

$refProp = new \ReflectionProperty(Test::class, 'property');
$refProp->setValue($test, 3);

var_dump($test->property);

Resulted in this output:

string(1) "3"

But I expected this output instead:

Fatal error: Uncaught TypeError: Cannot assign int to property Test::$property of type string

Link to 3v4l: https://3v4l.org/m0Te8

PHP Version

PHP >= 7.4

Operating System

Any

franmomu avatar Jan 12 '22 17:01 franmomu

PHP does not enable strict_types for its own actions. https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.strict

damianwadley avatar Jan 12 '22 20:01 damianwadley

thanks! I've been looking in docs about ReflectionProperty, but didn't look at strict types 👍

franmomu avatar Jan 12 '22 20:01 franmomu