php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79683 Fake reflection scope affects __toString()
Submitted: 2020-06-09 11:46 UTC Modified: 2020-06-09 13:49 UTC
From: smilimko at gmail dot com Assigned:
Status: Closed Package: Reflection related
PHP Version: 7.4.6 OS: Ubuntu
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: smilimko at gmail dot com
New email:
PHP Version: OS:

 

 [2020-06-09 11:46 UTC] smilimko at gmail dot com
Description:
------------
Incorrect collaboration of the reflection, magic method __toString and typed properties.

Test script:
---------------
class A
{
    private string $prop1 = '123';

    public function __toString()
    {
        return $this->prop1;
    }
}

class B
{
    private string $prop2;
}

$b = new B();

$reflector = new ReflectionClass($b);
$property = $reflector->getProperty('prop2');
$property->setAccessible(true);
$property->setValue($b, new A());


Expected result:
----------------
Property B::prop2 must have value '123'. No errors must happen.

Actual result:
--------------
PHP Fatal error:  Uncaught Error: Cannot access private property A::$prop1 in test.php:9
Stack trace:
#0 [internal function]: A->__toString()
#1 test.php(23): ReflectionProperty->setValue()
#2 {main}
  thrown in /home/sergey/www/twin.client/test.php on line 9


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-06-09 13:44 UTC] nikic@php.net
-Status: Open +Status: Verified
 [2020-06-09 13:44 UTC] nikic@php.net
Probably fake_scope leaking.
 [2020-06-09 13:49 UTC] nikic@php.net
-Summary: Strange behaviour of typed properties +Summary: Fake reflection scope affects __toString()
 [2020-06-09 13:53 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=2447fd9f84a484fbce8cd79e8e85b1fdf6ad7431
Log: Fixed bug #79683
 [2020-06-09 13:53 UTC] nikic@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 11:01:29 2024 UTC