huangdijia/invade

A PHP function to work with private properties and methods

v2.0.1 2023-09-21 00:17 UTC

This package is auto-updated.

Last update: 2024-07-21 02:27:59 UTC


README

Latest Test Latest Stable Version Latest Unstable Version Total Downloads License

This package offers an invade function that will allow you to read/write private properties of an object. It will also allow you to call private methods.

Installation

You can install the package via composer:

composer require huangdijia/invade

Usage

Imagine you have this class defined which has a private property and method.

<?php
use function Huangdijia\Invade\invade;

class MyClass
{
    private string $privateProperty = 'private value';

    private function privateMethod(): string
    {
        return 'private return value';
    }
}

$myClass = new MyClass();

This is how you can get the value of the private property using the invade function.

use function Huangdijia\Invade\invade;

invade($myClass)->privateProperty; // returns 'private value'

The invade function also allows you to change private values.

use function Huangdijia\Invade\invade;

invade($myClass)->privateProperty = 'changed value';
invade($myClass)->privateProperty; // returns 'changed value

Using invade you can also call private functions.

use function Huangdijia\Invade\invade;

invade($myClass)->privateMethod(); // returns 'private return value'

Testing

composer test
composer analyze

License

The MIT License (MIT). Please see License File for more information.