ucscode / object-reflector
A reflector that provides access to class encapsulated properties or methods
Requires
- php: >=8.1
This package is auto-updated.
Last update: 2026-03-11 09:41:57 UTC
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()andcall()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
ReflectionClassorReflectionMethodmanual 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.