|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81258 Strict mode not working with reflection
Submitted: 2021-07-14 13:27 UTC Modified: 2021-07-14 14:06 UTC
From: roke22 at gmail dot com Assigned:
Status: Open Package: Unknown/Other Function
PHP Version: 8.0.8 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: roke22 at gmail dot com
New email:
PHP Version: OS:


 [2021-07-14 13:27 UTC] roke22 at gmail dot com

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 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


class MyClassReflection {
    public int $name;

$dt = new MyClassReflection();
$dt->name = "33";


This one don't throw the exception but should throw the exception:

class MyClassReflection {
    public int $name;

$dt = new MyClassReflection();

$reflectionClass = new ReflectionClass('MyClassReflection');
$reflectionClass->getProperty('name')->setValue($dt, '33');



Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-07-14 13:39 UTC]
Property assignments follow the strict_type directive of wherever the assignment is performed from. In this case, it occurs from the ReflectionProperty::setValue() implementation, which is in strict_types=0 mode (like all extension code). Thus type coercions are allowed.

A way to perform the assignment with strict type semantics would be by using closure rebinding.
 [2021-07-14 14:06 UTC] roke22 at gmail dot com
excuse my ignorance, could you give a simple example? Maybe using the case of the example script.
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon Nov 28 12:05:52 2022 UTC