承接 ucscode/object-reflector 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

ucscode/object-reflector

最新稳定版本:v2.0.0

Composer 安装命令:

composer require ucscode/object-reflector

包简介

A reflector that provides access to class encapsulated properties or methods

README 文档

README

Surgical precision for PHP object manipulation.

ObjectReflector is a lightweight, fluent utility designed to bypass PHP encapsulation. It provides a clean API to interact with private and protected properties or methods without the boilerplate of the native Reflection API.

Optimized for PHP 8.1+ (No setAccessible() required).

🚀 Features

  • Fluent Interface: Chain multiple set() and call() operations in a single line.
  • Recursive Reflection: Use reflect() to dive into nested object properties instantly.
  • Modern PHP: Built to leverage PHP 8.1+ universal reflection access.
  • Zero Boilerplate: No more ReflectionClass or ReflectionMethod manual instantiation.

🛠 Installation

composer require ucscode/object-reflector

📖 Usage

1. Basic Manipulation (Fluent)

Reach into any object and modify its state, regardless of visibility.

use Ucscode\ObjectReflector\ObjectReflector;

$reflector = new ObjectReflector($object);

// Assuming "id" and "passwordHash" are private or protected properties
$reflector
    ->set('id', 123)
    ->set('passwordHash', $newHash)
    ->call('updateTimestamps'); // Assuming "updateTimestamps" is a private or protected method

2. Recursive Reflection

If a property contains another object, you can chain reflectors to access the child's internals without creating new variables.

$reflector
    ->reflect('invoice') // Assuming "invoice" is a private object
    ->reflect('client') // Assuming "client" is a private object inside "invoice"
    ->set('due', '$23.00'); // will set "object.invoice.client.due = $23.00"

3. Dynamic Invocation

Invoke methods using variadic arguments or arrays—perfect for hydrating objects from API payloads.

// Variadic
$reflector->call('logAction', 'User Update', 1);

// Array-based (useful for dynamic data)
$reflector->callArray('syncStatus', [
    'status' => 'active', 
    'notified' => true
]);

🎯 Why Use This?

  • Testing: Access private state in unit tests without adding public getters.
  • Hydration: Map database or API results to strictly encapsulated Domain Entities.
  • Legacy Support: Manipulate objects in third-party libraries that didn't provide enough hooks.

⚖️ License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固