2017 © Pedro Peláez
 

library liberator

A proxy for circumventing PHP visibility keyword restrictions.

image

eloquent/liberator

A proxy for circumventing PHP visibility keyword restrictions.

  • Sunday, August 14, 2016
  • by ezzatron
  • Repository
  • 1 Watchers
  • 26 Stars
  • 470,143 Installations
  • PHP
  • 34 Dependents
  • 0 Suggesters
  • 1 Forks
  • 2 Open issues
  • 7 Versions
  • 2 % Grown

The README.md

No longer maintained

This package is no longer maintained. See [this statement] for more info., (*1)

[this statement]: https://gist.github.com/ezzatron/713a548735febe3d76f8ca831bc895c0, (*2)

Liberator

A proxy for circumventing PHP access modifier restrictions., (*3)

Installation and documentation

What is Liberator?

Liberator allows access to protected and private methods and properties of objects as if they were marked public. It can do so for both objects and classes (i.e. static methods and properties)., (*4)

Liberator's primary use is as a testing tool, allowing direct access to methods that would otherwise require complicated test harnesses or mocking to test., (*5)

Usage

For objects

Take the following class:, (*6)

class SeriousBusiness
{
    private function foo($adjective)
    {
        return 'foo is ' . $adjective;
    }

    private $bar = 'mind';
}

Normally there is no way to call foo() or access $bar from outside the SeriousBusiness class, but Liberator allows this to be achieved:, (*7)

use Eloquent\Liberator\Liberator;

$object = new SeriousBusiness;
$liberator = Liberator::liberate($object);

echo $liberator->foo('not so private...'); // outputs 'foo is not so private...'
echo $liberator->bar . ' = blown';         // outputs 'mind = blown'

For classes

The same concept applies for static methods and properties:, (*8)

class SeriousBusiness
{
    static private function baz($adjective)
    {
        return 'baz is ' . $adjective;
    }

    static private $qux = 'mind';
}

To access these, a class liberator must be used instead of an object liberator, but they operate in a similar manner:, (*9)

use Eloquent\Liberator\Liberator;

$liberator = Liberator::liberateClass('SeriousBusiness');

echo $liberator->baz('not so private...'); // outputs 'baz is not so private...'
echo $liberator->qux . ' = blown';         // outputs 'mind = blown'

Alternatively, Liberator can generate a class that can be used statically:, (*10)

use Eloquent\Liberator\Liberator;

$liberatorClass = Liberator::liberateClassStatic('SeriousBusiness');

echo $liberatorClass::baz('not so private...');      // outputs 'baz is not so private...'
echo $liberatorClass::liberator()->qux . ' = blown'; // outputs 'mind = blown'

Unfortunately, there is (currently) no __getStatic() or __setStatic() in PHP, so accessing static properties in this way is a not as elegant as it could be., (*11)

Applications for Liberator

  • Writing white-box style unit tests (testing protected/private methods).
  • Modifying behavior of poorly designed third-party libraries.

The Versions

14/08 2016

v3.x-dev

3.9999999.9999999.9999999-dev https://github.com/eloquent/liberator

A proxy for circumventing PHP visibility keyword restrictions.

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

reflection access proxy object visibility private protected keyword modifier

09/02 2014

dev-develop

dev-develop https://github.com/eloquent/liberator

A proxy for circumventing PHP access modifier restrictions.

  Sources   Download

MIT

The Requires

 

The Development Requires

reflection access proxy object private protected modifier

09/02 2014

dev-master

9999999-dev https://github.com/eloquent/liberator

A proxy for circumventing PHP access modifier restrictions.

  Sources   Download

MIT

The Requires

 

The Development Requires

reflection access proxy object private protected modifier

09/02 2014

2.0.0

2.0.0.0 https://github.com/eloquent/liberator

A proxy for circumventing PHP access modifier restrictions.

  Sources   Download

MIT

The Requires

 

The Development Requires

reflection access proxy object private protected modifier

04/03 2013

1.1.1

1.1.1.0 https://github.com/eloquent/liberator

A proxy for circumventing PHP access modifier restrictions.

  Sources   Download

MIT

The Requires

 

The Development Requires

reflection access proxy object private protected modifier

02/08 2012

1.1.0

1.1.0.0 https://github.com/eloquent/liberator

A proxy for circumventing PHP access modifier restrictions.

  Sources   Download

MIT

The Requires

 

reflection access proxy object private protected modifier

02/08 2012

1.0.0

1.0.0.0 https://github.com/eloquent/liberator

A proxy for circumventing PHP access modifier restrictions.

  Sources   Download

MIT

The Requires

 

reflection access proxy object private protected modifier