Re: Problem with overwriting private property value with ReflectionProperty
| From: | Christian Stoller | Date: | Mon, 19 Sep 2016 08:38:16 +0000 |
| Subject: | Re: Problem with overwriting private property value with ReflectionProperty | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-96027@lists.php.net to get a copy of this message | ||
Von: Christian Stoller [mailto:stoller@leonex.de], Gesendet: Montag, 19. September 2016 10:27
Betreff: [PHP-DEV] Problem with overwriting private property value with ReflectionProperty
> Hi,
>
> I have spent several hours with a strange problem with reflection.
>
> There's an object of an external lib with a private field and I
> have to overwrite its values. Because there is no public method
> I am using ReflectionProperty like this:
>
> $reflProp = new \ReflectionProperty(Extension::class, 'classes');
> $reflProp->setAccessible(true);
> $reflProp->setValue($extension,['new', 'values']);
>
> var_export($extension->getClasses());
>
> The field is of type array and contains several entries. The problem
> is, that the private field has not been overwritten by
>
$reflProp->setValue(). Instead it looked like, the array of
> the
> private field has been merged with my new array.
>
> I tried different things but nothing worked. I have tried to create
> a small script which could show the problem, but it wasn't reproducible.
>
> The last thing I tried was, to call
>
> var_export($reflProp->getValue($extension));
>
> instead of
>
> var_export($extension->getClasses());
>
> That returns exactly the correct value. So my question is if somebody
> could imagine why $reflProp->getValue() returns the value I
> am trying
> to set, but accessing the field with an object member method
> (getClasses) returns a merged array.
>
> I am using PHP 5.6.26 (x86, TS) on Windows 10.
>
> Best regards,
> Christian
Oh, sorry. I have found the problem now, after trying to overwrite the value with a string instead
of an array. The object I had the problem with, has been extended by another class the the
getClasses merged the field with another array.
Sorry for the noise.
Christian
Mit freundlichen Gr??en aus Paderborn
Christian Stoller
Softwareentwicklung
LEONEX Internet GmbH
Technologiepark 20
33100 Paderborn
Tel: 05251-14807-27
Fax: 05251-14807-30
HRB 8694 AG Paderborn
Gesch?ftsf?hrer: Stephan Winter
________________________________
Erfolgreiche E-Business L?sungen von LEONEX
Erfahren Sie mehr unter www.leonex.de
________________________________