danielgsims/staticproxy 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

danielgsims/staticproxy

最新稳定版本:1.0.2

Composer 安装命令:

composer require danielgsims/staticproxy

包简介

A thin proxy for static classes

README 文档

README

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.


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:

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.

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.

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

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

统计信息

  • 总下载量: 8.19k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2014-11-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固