2017 © Pedro Peláez
 

library staticproxy

A thin proxy for static classes

image

danielgsims/staticproxy

A thin proxy for static classes

  • Monday, December 21, 2015
  • by danielgsims
  • Repository
  • 1 Watchers
  • 10 Stars
  • 4,201 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 6 Versions
  • 3 % Grown

The README.md

StaticProxy

StaticProxy is a thin class that acts as a proxy for calling static methods. You can use StaticProxy when you would like to inject an instance as opposed to calling static methods directly., (*1)


class Foo { public static function doSomething() { return "do something"; } } $s = new StaticProxy("Foo"); $s->doSomething();

Why?

Sometimes, you have a dependency that only functions with static methods:, (*2)

class MyController
{
    public function index(Request $request)
    {
        Validator::validate($request);

        //do something
    }
}

Ideally, you would want to avoid static methods to make your code loosely coupled. You can use a proxy to make this happen., (*3)

class MyController
{
    private $validator;

    public function __construct(StaticProxy $validator)
    {
        $this->validator = $validator;
    }

    public function index(Request $request)
    {
        $this->validator->validate($request);
    }
}

This class is useful when you need to decouple static calls from your code but are unable to rewrite your dependencies, or do not have the means to create individual adapters., (*4)

Alias

In the above example, we had to typehint that we were using a static proxy, but that's not ideal. Instead, we can use the Aliaser to register aliases of StaticProxy., (*5)

class MyController
{
    private $validator;

    public function __construct(Acme\Validator $validator)
    {
        $this->validator = $validator;
    }

    public function index(Request $request)
    {
        $this->validator->validate($request);
    }
}

$a = new Aliaser;
$a->register("Acme\Validator");

$s = new StaticProxy("Foo");
$c = new MyController($s);

The Versions

21/12 2015

dev-master

9999999-dev

A thin proxy for static classes

  Sources   Download

The Development Requires

21/12 2015

1.0.2

1.0.2.0

A thin proxy for static classes

  Sources   Download

The Development Requires

19/11 2014

1.0.1

1.0.1.0

A thin proxy for static classes

  Sources   Download

The Development Requires

19/11 2014

0.2.0

0.2.0.0

A thin proxy for static classes

  Sources   Download

The Development Requires

19/11 2014

1.0.0

1.0.0.0

A thin proxy for static classes

  Sources   Download

The Development Requires

18/11 2014

0.1.0

0.1.0.0

A thin proxy for static classes

  Sources   Download

The Development Requires