PHPackages                             leedavis81/altr-ego - 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. leedavis81/altr-ego

ActiveLibrary

leedavis81/altr-ego
===================

Access an objects protected / private properties and methods

v1.0.2(12y ago)221.2k[1 PRs](https://github.com/leedavis81/altr-ego/pulls)MITPHPPHP &gt;=5.3.3

Since May 8Pushed 9y ago2 watchersCompare

[ Source](https://github.com/leedavis81/altr-ego)[ Packagist](https://packagist.org/packages/leedavis81/altr-ego)[ RSS](/packages/leedavis81-altr-ego/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (4)Used By (0)

Create an AltrEgo
=================

[](#create-an-altrego)

[![Build Status](https://camo.githubusercontent.com/159abdceb863d8b71cd269fa6346d7f6f8f4761e5cca21a99f12142a2dcc55ea/68747470733a2f2f7472617669732d63692e6f72672f6c6565646176697338312f616c74722d65676f2e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/leedavis81/altr-ego)[![Total Downloads](https://camo.githubusercontent.com/cd43ac20b083ac19935e4213cf46da51983679b6b4dbfc9f149b27a2b1fc4aae/68747470733a2f2f706f7365722e707567782e6f72672f6c6565646176697338312f616c74722d65676f2f642f746f74616c2e706e67)](https://packagist.org/packages/leedavis81/altr-ego)[![Latest Stable Version](https://camo.githubusercontent.com/e6935f1d99706c8ede6f770340a2cf6cee6755d977750f76b7b4ec6fb79b760b/68747470733a2f2f706f7365722e707567782e6f72672f6c6565646176697338312f616c74722d65676f2f76657273696f6e2e706e67)](https://packagist.org/packages/leedavis81/altr-ego)

A tool to allow you access to an object's protected / private properties by breaking PHP scope. This is useful in testing scenarios where you want to quickly verify a hidden encapsulated routine within your application. Often when you just want to test a small part of your application, that routine can be (rightly so) encapsulated and set to private / protected. And is uncallable from your test suite. Running through the process of creating, mocking and injecting dependencies just to get your object in a valid state for your test can be very timely. This tool will help you call that private routine or inspect that protected property directly.

AltrEgo allows you to completely maintain your object's state throughout any manipulations. If you decide you want the scope to come back into play, you simply fetch your object back. Any changes made during its time as an "AltrEgo" object will remain.

This library uses adapters for different versions of PHP.

If you're using 5.3
-------------------

[](#if-youre-using-53)

Then PHP's Reflection classes are used to break the scope of your object.

For 5.4 this library uses closure scope binding
-----------------------------------------------

[](#for-54-this-library-uses-closure-scope-binding)

PHP 5.4 has a new "scope breaking" feature with the use of closures. Take a look at [Davey Shafik's closure puzzle blog post](http://daveyshafik.com/archives/32789-the-closure-puzzle.html) top get a good understanding of how this works. This method is **far quicker** than using PHP's built in Reflection tools. Tests I've performed "breaking scope" this way have given a speed increase in of around 52%.

Usage
-----

[](#usage)

```
// Given the following class (nice and private all round)
class Foo
{
    private $priv = 'This is a private variable';

    private $privArray = array('private array entry');

    private function privFunc($value)
    {
        return $value;
    }

    private static function privStatFunc($value)
    {
        return $value;
    }
}

// We first off create an alter ego
$alterEgo = AltrEgo::create(new Foo());

// Right, now lets get a hook on those private bits
echo $alterEgo->priv . PHP_EOL;
// Output: This is a private variable

// Woot, works, now lets set it to something else and see what happens
$alterEgo->priv = 'new private value';
echo $alterEgo->priv . PHP_EOL;
// Output: new private value

// This value remains on your object, even after retrieving it back with $alterEgo->getObject()
// now lets try some method calls
echo $alterEgo->privFunc('Private call') . PHP_EOL;
//Output: Private call

// Now then, onto arrays; You can pass in an array of parameters like so:
var_dump($alterEgo->privFunc('Private', 'call'));
/**
Output:
array(2) {
  [0]=>
  string(7) "Private"
  [1]=>
  string(4) "call"
}
*/

// You can push values straight into them using standard PHP array syntax.
// But be aware, the array will be converted (and maintained) as an ArrayObject
$alterEgo->privArray[] = 'new value';
var_dump($alterEgo->privArray);
/**
Output:
object(ArrayObject)#5 (1) {
  ["storage":"ArrayObject":private]=>
  array(2) {
    [0]=>
    string(19) "private array entry"
    [1]=>
    string(9) "new value"
  }
}
*/

// You can add associative array values and unset them as you normally would in PHP
$alterEgo->privArray['assoc_key'] = 'private key value entry';
var_dump($alterEgo->privArray);
unset($alterEgo->privArray['assoc_key']);
var_dump($alterEgo->privArray);
/**
Output:
object(ArrayObject)#5 (1) {
  ["storage":"ArrayObject":private]=>
  array(3) {
    [0]=>
    string(19) "private array entry"
    [1]=>
    string(9) "new value"
    ["assoc_key"]=>
    string(23) "private key value entry"
  }
}
object(ArrayObject)#5 (1) {
  ["storage":"ArrayObject":private]=>
  array(2) {
    [0]=>
    string(19) "private array entry"
    [1]=>
    string(9) "new value"
  }
}
*/

// We also have the facility to execute static function that have private/protected visibility.
// It's rare you'll ever come across these, they're typically set as public so you'd normally call them directly
echo AltrEgo::callStatic($alterEgo, 'privStatFunc', 'private static call') . PHP_EOL;
// Output: private static call

// Also works with arrays
var_dump(AltrEgo::callStatic($alterEgo, 'privStatFunc', array('private', 'static', 'call')));
/**
Output:
array(3) {
  [0]=>
  string(7) "private"
  [1]=>
  string(6) "static"
  [2]=>
  string(4) "call"
}
*/

// If at anytime you want to jump back into scope just fetch your object back.
// You can throw it back into AltrEgo::create() whenever you need
$backToScope = $alterEgo->getObject();
```

Limitations
-----------

[](#limitations)

1. Whenever accessing an array property it will be converted (and maintained) as an ArrayObject. This is due to limitation on setting array values when using PHP overloading (\_\_get). If this is a problem you can either get the value directly, then overwrite it with a modified one. Or once you've retrieved the modified ArrayObject simply run -&gt;toArray() on it.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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 ~39 days

Total

3

Last Release

4674d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f124d2489389e8961373ab602ebf50a8b7c65ed185fc2e748a2431cb81307c9e?d=identicon)[leedavis81](/maintainers/leedavis81)

---

Top Contributors

[![leedavis81](https://avatars.githubusercontent.com/u/624973?v=4)](https://github.com/leedavis81 "leedavis81 (36 commits)")

---

Tags

phpbreak scope

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/leedavis81-altr-ego/health.svg)

```
[![Health](https://phpackages.com/badges/leedavis81-altr-ego/health.svg)](https://phpackages.com/packages/leedavis81-altr-ego)
```

###  Alternatives

[pestphp/pest-plugin-stressless

Stressless plugin for Pest

67792.6k16](/packages/pestphp-pest-plugin-stressless)

PHPackages © 2026

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