hakito/publisher 问题修复 & 功能扩展

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

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

hakito/publisher

Composer 安装命令:

composer require hakito/publisher

包简介

Proxy for accessing protected/private class members.

README 文档

README

Build Status Coverage Status Latest Stable Version Total Downloads Latest Unstable Version License

Simple proxy for accessing protected/private class members.

It's intention is to give unit tests access to private members without reinventing the wheel.

This helper uses closures to access private members of a class instead of reflection.

Installation

composer require hakito/publisher

Usage

Guess you have a class with private members:

class Target
{
    private $foo = 'secret';
    private function bar($arg) { return $arg . 'Bar'; }

    private static $sFoo = 'staticSecret';
}

Create the proxy to access these members:

$target = new Target();
$published = new hakito\Publisher\Published($target);

// Get private property
$property = $published->foo;
// $property = 'secret';

// Set private property
$published->foo = 'outsider';
// $target->foo = 'outsider';

// call private method
$name = $published->bar('Saloon');
// $name = 'SaloonBar';


// Optional you can provide a base class in the constructor
class Derived extends Target {
    private $foo = 'derived';
}

$derived = new Derived();
$published = new hakito\Publisher\Published($derived, Target::class);
$property = $published->foo; // Gets property from Target
// $property = 'secret';

Accessing static members

If you want to access static fields or methods you have to use the class StaticPublished

$published = new StaticPublished(Target::class);

$property = $published->sFoo;
// $property = 'staticSecret'

Setting fields and calling methods works the same as for instances.

Limitations

The published method call cannot set a reference argument.

class Target
{
    private function methodWithReferenceArgument(&$arg) { $arg = 'hi'; }
}

$target = new Target();
$published = new hakito\Publisher\Published($target);
$val = 'initial';
$published->methodWithReferenceArgument($val);
// $val is still 'initial'

The return value from method call cannot be a reference

class Target
{
    private $_member = [];
    private function &methodWithReferenceReturnValue($arg) { return $this->_member; }
}

$target = new Target();
$published = new hakito\Publisher\Published($target);
$val = 'initial';
$published->methodWithReferenceReturnValue($val)['new'] = 'value';
// Target::_member is still []

统计信息

  • 总下载量: 193
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-03-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固