huangdijia / invade
A PHP function to work with private properties and methods
v2.0.1
2023-09-21 00:17 UTC
Requires
- php: ^8.1
Requires (Dev)
- huangdijia/php-coding-standard: ^1.1
- pestphp/pest: ^2.0
- phpstan/phpstan: ^1.10
This package is auto-updated.
Last update: 2024-10-21 03:07:29 UTC
README
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.