php.net |  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
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: roke22 at gmail dot com
New email:
PHP Version: OS:

 

 [2021-07-14 13:27 UTC] roke22 at gmail dot com
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);


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-07-14 13:39 UTC] nikic@php.net
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.
 [2023-07-25 06:47 UTC] faulknerehman at gmail dot com
When you use direct assignment like $dt->name = "33"; in PHP, it performs a type check and throws an error if the type is not compatible with the property declaration. However, when you use Reflection to set a value, PHP does not perform strict type checking by default. This behavior is by design and allows you to bypass type checking when using Reflection.

If you want to enforce strict type checking even when using Reflection, you can manually enable it by calling declare(strict_types=1) inside the method that uses Reflection. Here's how you can modify the second example to throw an error if the type is not compatible:


declare(strict_types=1);

class MyClassReflection {
    public int $name;
}

$dt = new MyClassReflection();

$reflectionClass = new ReflectionClass('MyClassReflection');
$property = $reflectionClass->getProperty('name');
$property->setAccessible(true); // Make the property accessible, as it is not public
$property->setValue($dt, '33'); // Throws TypeError since '33' is a string, not an int

var_dump($dt);

(https://github.com.php)(https://www.ballsportsgames.com/how-to-choose-pickleball-paddles/)
By using declare(strict_types=1) in the scope where you are working with Reflection, you enforce strict typing for that part of the code, and setting a value of the wrong type will throw a TypeError, just like in the first example.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC