Private Access

Packagist GitHub Gitlab Bitbucket

A small simple library to access private properties of the objects. Actually it’s more an example of mad skillz than a useful tool. No Reflection API calls!

Installation

Use composer:

composer require sandfoxme/private-access --dev

Why --dev?

This library may be used for debugging or with PHP consoles like PsySH. If you are actually using it in some live system, you’re doing something terribly wrong.

Functions

These four simple functions can come in handy as helpers for something like PsySH

Note

All functions also work with protected and public visibilities

get_private_field

Get value of a private field

<?php

use function SandFox\Debug\get_private_field;

get_private_field($a,       'secret'); // get $a->secret value
get_private_field(A::class, 'secret'); // get A::$secret value

set_private_field

Set value of a private field

<?php

use function SandFox\Debug\set_private_field;

set_private_field($a,       'secret', 'new secret'); // set new $a->secret value
set_private_field(A::class, 'secret', 'new secret'); // set new A::$secret value

call_private_method

Call private method

<?php

use function SandFox\Debug\call_private_method;

call_private_method($a,       'doStuff', 'whatever'); // call $a->doStuff('whatever')
call_private_method(A::class, 'doStuff', 'whatever'); // call A::doStuff('whatever')

get_private_const

Get value of a private constant

Note

This function works in all supported PHP versions but is useful only with PHP 7.1+ where constants can be private or protected

<?php

use function SandFox\Debug\get_private_const;

get_private_const($a,       'SECRET_CONST');
// or
get_private_const(A::class, 'SECRET_CONST');

License

The library is available as open source under the terms of the MIT License.