定制 vedavith/liberator-fork 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

vedavith/liberator-fork

最新稳定版本:v4.2.0

Composer 安装命令:

composer require vedavith/liberator-fork

包简介

A proxy for circumventing PHP access modifier restrictions.

README 文档

README

A proxy for circumventing PHP access modifier restrictions.

  • This is a maintained fork of the unmaintained eloquent/liberator. Package name is vedavith/liberator-fork and it targets PHP 8.2+ with PHPUnit 11/12 for development.
  • This is a maintained fork of [eloquent/liberator] originally created by Erin Millard.

Installation and documentation

  composer require vedavith/liberator-fork

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

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.

Usage

For objects

Take the following class:

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:

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:

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:

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:

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.

Applications for Liberator

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固