php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80564 ReflectionProperty::__toString() renders current value, not default value
Submitted: 2020-12-30 04:43 UTC Modified: 2021-07-23 07:46 UTC
From: david at grudl dot com Assigned: nikic (profile)
Status: Closed Package: Reflection related
PHP Version: 8.0.1RC1 OS:
Private report: No CVE-ID: None
 [2020-12-30 04:43 UTC] david at grudl dot com
Description:
------------
ReflectionProperty::__toString() convert also value of property to string and it may fail, see https://3v4l.org/rfoGl



Test script:
---------------
<?php

class A
{
	public static $x;
}

A::$x = new stdClass;

$rp = new ReflectionProperty('A', 'x');
echo $rp;

Expected result:
----------------
Property [ public static $x ]

Actual result:
--------------
Fatal error: Uncaught Error: Object of class stdClass could not be converted to string

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-12-30 06:30 UTC] requinix@php.net
-Summary: ReflectionProperty::__toString() converts property value to string +Summary: ReflectionProperty::__toString() renders current value, not default value -Status: Open +Status: Verified
 [2020-12-30 06:30 UTC] requinix@php.net
Fails during

zval *default_value = property_get_default(prop);
if (!Z_ISUNDEF_P(default_value)) {
    smart_str_appends(str, " = ");
    if (format_default_value(str, default_value, prop->ce) == FAILURE) {
        return;
    }
}

Apparently it's trying to get a default value but is actually coming up with the current value.
https://3v4l.org/g4OLG
 [2021-07-23 07:46 UTC] nikic@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: nikic
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 10:01:28 2024 UTC