Bug #81258 [NEW]: Strict mode not working with reflection
| From: | roke22 at gmail dot com | Date: | Wed, 14 Jul 2021 13:27:34 +0000 |
| Subject: | Bug #81258 [NEW]: Strict mode not working with reflection | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-235027@lists.php.net to get a copy of this message | ||
From: roke22 at gmail dot com
Operating system: Linux
PHP version: 8.0.8
Package: Unknown/Other Function
Bug Type: Bug
Bug description:Strict mode not working with reflection
Description:
------------
Hello,
when you try to assign a value to a property of a DTO and it is not of
the same type, it fails when you do it normally.
However this does not happen when you use reflection, in this case try
to convert it to the type of the property making for example a 2 to be
true for a bool property.
The expected behavior should be the same. This error throws all
libraries about dto like https://github.com/spatie/data-transfer-object
because you can get the DTO with a true or false value in boolean
properties where are invalid.
Test script:
---------------
This one fails and works ok
<?php
declare(strict_types=1);
class MyClassReflection {
public int $name;
}
$dt = new MyClassReflection();
$dt->name = "33";
var_dump($dt);
This one don't throw the exception but should throw the exception:
<?php
declare(strict_types=1);
class MyClassReflection {
public int $name;
}
$dt = new MyClassReflection();
$reflectionClass = new ReflectionClass('MyClassReflection');
$reflectionClass->getProperty('name')->setValue($dt, '33');
var_dump($dt);
--
Edit bug report at https://bugs.php.net/bug.php?id=81258&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=81258&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=81258&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=81258&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=81258&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=81258&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=81258&r=support
Expected behavior: https://bugs.php.net/fix.php?id=81258&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=81258&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=81258&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=81258&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=81258&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=81258&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=81258&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=81258&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=81258&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=81258&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=81258&r=mysqlcfg