PHPackages                             spatie/invade - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. spatie/invade

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

spatie/invade
=============

A PHP function to work with private properties and methods

2.1.0(2y ago)34027.0M—2.5%18[1 PRs](https://github.com/spatie/invade/pulls)20MITPHPPHP ^8.0CI passing

Since Feb 11Pushed 1mo ago4 watchersCompare

[ Source](https://github.com/spatie/invade)[ Packagist](https://packagist.org/packages/spatie/invade)[ Docs](https://github.com/spatie/invade)[ GitHub Sponsors](https://github.com/spatie)[ RSS](/packages/spatie-invade/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (14)Used By (20)

[![Social Card of Invade](/art/socialcard.png)](/art/socialcard.png)

A PHP function to access private properties and methods
=======================================================

[](#a-php-function-to-access-private-properties-and-methods)

[![Latest Version on Packagist](https://camo.githubusercontent.com/0e34cf74b42f0ca8b6ff314ee5a7730d163561d1b6c41bcce6b95d674e825d83/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f696e766164652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/invade)[![Tests](https://github.com/spatie/invade/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/spatie/invade/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/c6466425d8d79ea3158446207294081c69365ab63b2734c85464a5f816ebd80e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f696e766164652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/invade)

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
----------

[](#support-us)

[![](https://camo.githubusercontent.com/bd98a79a1b38da76159d9bdc3dda845c26083283cf034ab562e8bd59dda60f19/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f696e766164652e6a70673f743d31)](https://spatie.be/github-ad-click/invade)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

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](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/invade
```

Usage
-----

[](#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'
```

Further, you can also get and set private static class properties and call private static methods. Imagine having this class:

```
class MyClass
{
    private static string $privateStaticProperty = 'privateValue';

    private static function privateStaticMethod(string $string, int $int): string
    {
        return 'private return value ' . $string . ' ' . $int;
    }
}
```

Here is how you get and set private class properties:

```
invade(MyClass::class)->get('privateStaticProperty'); // returns 'private value'

invade(MyClass::class)->set('privateStaticProperty', 'changedValue');

invade(MyClass::class)->get('privateStaticProperty'); // returns 'changedValue'
```

And this is how you call private static methods:

```
invade(MyClass::class)
    ->method('privateStaticMethod')
    ->call('foo', 123);

// returns 'private return value foo 123'
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/spatie)
- [All Contributors](../../contributors)

And a special thanks to [Caneco](https://twitter.com/caneco) for the logo ✨

The [original idea](https://twitter.com/calebporzio/status/1492141967404371968) for the `invade` function came from [Caleb "string king" Porzio](https://twitter.com/calebporzio). We slightly polished the code that he created in [this commit on Livewire](https://github.com/livewire/livewire/pull/4649/files).

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

60

—

FairBetter than 99% of packages

Maintenance60

Regular maintenance activity

Popularity68

Solid adoption and visibility

Community41

Growing community involvement

Maturity61

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~91 days

Recently: every ~177 days

Total

10

Last Release

731d ago

Major Versions

0.0.2 → 1.0.02022-02-11

1.1.1 → 2.0.02023-07-19

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (40 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (20 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (15 commits)")[![enricodelazzari](https://avatars.githubusercontent.com/u/10452445?v=4)](https://github.com/enricodelazzari "enricodelazzari (14 commits)")[![jrmajor](https://avatars.githubusercontent.com/u/26096713?v=4)](https://github.com/jrmajor "jrmajor (6 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (4 commits)")[![otsch](https://avatars.githubusercontent.com/u/4062813?v=4)](https://github.com/otsch "otsch (4 commits)")[![parastooam](https://avatars.githubusercontent.com/u/28684227?v=4)](https://github.com/parastooam "parastooam (3 commits)")[![danharrin](https://avatars.githubusercontent.com/u/41773797?v=4)](https://github.com/danharrin "danharrin (3 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![caneco](https://avatars.githubusercontent.com/u/502041?v=4)](https://github.com/caneco "caneco (2 commits)")[![tpetry](https://avatars.githubusercontent.com/u/315686?v=4)](https://github.com/tpetry "tpetry (1 commits)")[![Ayoub-Mabrouk](https://avatars.githubusercontent.com/u/77799760?v=4)](https://github.com/Ayoub-Mabrouk "Ayoub-Mabrouk (1 commits)")[![benjam-es](https://avatars.githubusercontent.com/u/1738602?v=4)](https://github.com/benjam-es "benjam-es (1 commits)")

---

Tags

consenting-adultsphpspatieinvade

###  Code Quality

TestsPest

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/spatie-invade/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-invade/health.svg)](https://phpackages.com/packages/spatie-invade)
```

###  Alternatives

[spatie/laravel-package-tools

Tools for creating Laravel packages

935125.5M7.0k](/packages/spatie-laravel-package-tools)[spatie/laravel-data

Create unified resources and data transfer objects

1.7k28.9M627](/packages/spatie-laravel-data)[spatie/laravel-analytics

A Laravel package to retrieve Google Analytics data.

3.2k5.7M57](/packages/spatie-laravel-analytics)[spatie/macroable

A trait to dynamically add methods to a class

72659.6M64](/packages/spatie-macroable)[spatie/regex

A sane interface for php's built in preg\_\* functions

1.1k17.1M59](/packages/spatie-regex)[spatie/enum

PHP Enums

84429.1M68](/packages/spatie-enum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
