spatie/invade

A PHP function to work with private properties and methods

Fund package maintenance!
spatie

Installs: 550 415

Dependents: 54

Suggesters: 0

Security: 0

Stars: 224

Watchers: 3

Forks: 13

1.1.1 2022-07-05 09:31 UTC

This package is auto-updated.

Last update: 2023-01-30 05:05:21 UTC


README

68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f737570706f72742d756b7261696e652e7376673f743d31

Social Card of Invade

A PHP function to access private properties and methods

Latest Version on Packagist Tests Total Downloads

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.

Support us

68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f696e766164652e6a70673f743d31

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/invade

Usage

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

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.

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

The invade function also allows you to change private values.

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

Using invade you can also call private functions.

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

Making PHPStan understand Invade

PHPStan will report errors for every invaded private method and property as it is not aware that you can now access them. To remove these errors install the PHPStan extension installer or add the invade PHPStan extension manually to your PHPStan configuration:

includes:
    - ./vendor/spatie/invade/phpstan-extension.neon

Testing

composer test
vendor/bin/phpstan analyse -c types/phpstan.neon.dist

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

And a special thanks to Caneco for the logo ✨

The original idea for the invade function came from Caleb "string king" Porzio. We slightly polished the code that he created in this commit on Livewire.

License

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