php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80821 ReflectionProperty::getDefaultValue() returns current value for statics
Submitted: 2021-03-02 23:17 UTC Modified: 2021-07-23 07:30 UTC
From: dktapps at pmmp dot io Assigned: nikic (profile)
Status: Closed Package: Reflection related
PHP Version: 8.0.2 OS: Linux
Private report: No CVE-ID: None
 [2021-03-02 23:17 UTC] dktapps at pmmp dot io
Description:
------------
When OPcache is not present, default_static_members_table gets written to directly  instead of being copied.
I'm not sure why this is; at best it seems like a premature optimisation, especially considering that OPcache should be used in the 99% case.

This problem has plagued ext-pthreads and other threading extensions for many years (since even before PHP 7.0), creating inconsistencies of behaviour when OPcache is used, vs when it is not.

This can finally be seen in userland in PHP 8.0 with the script below, which demonstrates the problem using reflection.

Side note: A similar problem/inconsistency also exists with function static_variables, but these can't be inspected using reflection, since PHP doesn't offer any reflection API to see the default values of static variables. (This is primarily a problem for ext-pthreads class copying.)

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

class Statics{
	public static $staticVar = 1;
}

Statics::$staticVar = 2;

$reflect = new \ReflectionClass(Statics::class);
$prop = $reflect->getProperty("staticVar");
var_dump($prop->getDefaultValue());


Expected result:
----------------
int(1)

Actual result:
--------------
WITH OPcache:
int(1)

WITHOUT OPcache:
int(2)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-07-23 07:30 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC