2017 © Pedro Peláez
 

library andrew

Proxy objects to access (dynamic and static) private properties and methods.

image

gmazzap/andrew

Proxy objects to access (dynamic and static) private properties and methods.

  • Sunday, March 12, 2017
  • by gmazzap
  • Repository
  • 3 Watchers
  • 5 Stars
  • 1,840 Installations
  • PHP
  • 8 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 6 % Grown

The README.md

Andrew

Proxy objects to access (dynamic and static) private properties and methods., (*1)


travis-ci status codecov.io license, (*2)


What

Andrew allow to access (read & write) private properties and methods of any objects., (*3)

It provides 2 "proxy" objects, one for dynamic properties and methods, the other for static properties and method., (*4)

Proxy

Let's assume following class, (*5)

class Subject
{
  private $var = 'I am private';

  public function getVar() {
      return $this->var;
  }

  public function hasVar() {
      return isset($this->var);
  }

  private function testMe() {
      return 'I am private';
  }
}

With Andrew is possible to:, (*6)

$subject = new Subject();

$proxy = new Andrew\Proxy($subject);

// get subject private var via proxy
echo $proxy->var; // 'I am private'

// set subject private var via proxy
$proxy->var = 'I WAS private';
echo $subject->getVar(); // 'I WAS private'

// check subject private var is set via proxy
isset($proxy->var); // true

// unset subject private var via proxy
unset($proxy->var);
$subject->hasVar(); // false

// call subject private method via proxy
echo $proxy->testMe() // 'I am private'

The Subject class example has public getter and isser for its private variable, that we added for test purposes, bu Andrew proxy is, of course, more useful when classes does not provide that possibility., (*7)

Static Proxy

Let's assume following class, (*8)

class Subject
{
    private static $var = 'I am private static';

    public static function getVar() {
        return self::$var;
    }

    private static function testMe() {
        return 'I am private static';
    }
}

With Andrew is possible to:, (*9)

$proxy = new Andrew\StaticProxy(Subject::class); // we pass class name, not object

// getter
echo $proxy->var; // 'I am private static'

// setter
$proxy->var = 'I WAS private static';
echo Subject::getVar(); // 'I WAS private static'

// isser
isset($proxy->var); // true

// caller
echo $proxy->testMe() // 'I am private static'

Note that StaticProxy has not unsetter, because PHP does not allow to unset static variables., (*10)

If you try to unset anything on a StaticProxy object, Andrew will throw an Exception., (*11)

Exceptions

There are several exceptions thrown by Andrew. All of them are in the namespace Andrew\Exception., (*12)

They are:, (*13)

  • ArgumentException thrown when an invalid type of argument is used. E.g. when a method expects a string and receives a number or anything else.
  • ClassException thrown when a method expects a class but receives a string that is not a valid class name.
  • PropertyException when trying to access a non-existent variable, or either when trying to access a static variable using Proxyor a non-static property using StaticProxy.
  • MethodException when trying to access a non-existent method, or either when trying to access a static method using Proxyor a non-static method using StaticProxy.
  • RuntimeException when trying to unset a static variable

Installation

Install via Composer, package name is "gmazzap/andrew" and it is available on Packagist., (*14)

Should I use this in production?

No. But may be quite useful in unit tests., (*15)

Requirements

Andrew has no dependencies. Requires PHP 5.6+, works with PHP 7 and 7.1., (*16)

License

MIT, (*17)

The Versions

12/03 2017

dev-master

9999999-dev https://github.com/Giuseppe-Mazzapica/Andrew

Proxy objects to access (dynamic and static) private properties and methods.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

unit tests proxy objects

12/03 2017

2.1.0

2.1.0.0 https://github.com/Giuseppe-Mazzapica/Andrew

Proxy objects to access (dynamic and static) private properties and methods.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

unit tests proxy objects

12/03 2017

2.0.0

2.0.0.0 https://github.com/Giuseppe-Mazzapica/Andrew

Proxy objects to access (dynamic and static) private properties and methods.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

unit tests proxy objects

12/03 2017

dev-release/2

dev-release/2 https://github.com/Giuseppe-Mazzapica/Andrew

Proxy objects to access (dynamic and static) private properties and methods.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

unit tests proxy objects

15/09 2016

1.1.0

1.1.0.0 https://github.com/Giuseppe-Mazzapica/Andrew

Proxy objects to access (dynamic and static) private properties and methods.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

unit tests proxy objects

07/07 2015

1.0.1

1.0.1.0 https://github.com/Giuseppe-Mazzapica/Andrew

Proxy objects to access (dynamic and static) private properties and methods.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

unit tests proxy objects

07/07 2015

1.0.0

1.0.0.0 https://github.com/Giuseppe-Mazzapica/Andrew

Proxy objects to access (dynamic and static) private properties and methods.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

unit tests proxy objects