Is there any way to set a private/protected static property using reflection classes?

后端 未结 2 1420
广开言路
广开言路 2020-12-24 00:31

I am trying to perform a backup/restore function for static properties of classes. I can get a list of all of the static properties and their values using the reflection obj

2条回答
  •  滥情空心
    2020-12-24 00:58

    For accessing private/protected properties of a class we may need to set the accessibility of that class first, using reflection. Try the following code:

    $obj         = new ClassName();
    $refObject   = new ReflectionObject( $obj );
    $refProperty = $refObject->getProperty( 'property' );
    $refProperty->setAccessible( true );
    $refProperty->setValue(null, 'new value');
    

提交回复
热议问题