Simple and fast methods to read private properties and call private methods
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.gitignore
.travis.yml
LICENSE.md
README.md
composer.json

README.md

PHP Private Access

Packagist Packagist Travis

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!

Usage

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

<?php

use function SandFoxMe\Debug\get_private_field;
use function SandFoxMe\Debug\set_private_field;
use function SandFoxMe\Debug\call_private_method;
use function SandFoxMe\Debug\get_private_const;

get_private_field($a, 'secret'); // get $a->secret value
set_private_field($a, 'secret', 'new secret'); // set new $a->secret value
call_private_method($a, 'doStuff', 'whatever'); // call $a->doStuff('whatever')

// Use class name to do static!

get_private_field(A::class, 'secret'); // get A::$secret value
set_private_field(A::class, 'secret', 'new secret'); // set new A::$secret value
call_private_method(A::class, 'doStuff', 'whatever'); // call A::doStuff('whatever')

// In PHP 7.1 you can also access private constants

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

Installation

Add the following to your composer.json:

{
    "require-dev": {
        "sandfoxme/private-access": "*"
    }
}

Why "require-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.

License

MIT, see LICENSE.md