Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save consatan/009f505c436d58fd48a7c2ea414b0996 to your computer and use it in GitHub Desktop.
Save consatan/009f505c436d58fd48a7c2ea414b0996 to your computer and use it in GitHub Desktop.
PHP set private property value using reflection. This allows you to set a private property value from outside the object, great for PHPUnit testing.
<?php
$a = new A();
$reflection = new \ReflectionClass($a);
$property = $reflection->getProperty('privateProperty');
$property->setAccessible(true);
$property->setValue($a, 'new-value');
echo $a->getPrivateProperty();
//outputs:
//new-value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment