Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Object Reflector Laravel Package

sebastian/object-reflector

Reflect object properties across a class hierarchy, including inherited, private, and protected members. sebastian/object-reflector provides a small utility for accessing and inspecting object state when PHP’s standard reflection APIs are inconvenient.

View on GitHub
Deep Wiki
Context7

Getting Started

Install the package as a dev dependency to avoid runtime overhead:

composer require --dev sebastian/object-reflector

Begin by inspecting private/protected properties in tests or debugging sessions. The API is minimal and self-explanatory:

use SebastianBergmann\ObjectReflector\ObjectReflector;

$reflector = new ObjectReflector();
$properties = $reflector->getProperties($someObject);

// Output all properties and their values
foreach ($properties as $name => $value) {
    echo "$name = " . print_r($value, true) . PHP_EOL;
}

This is your go-to when Laravel’s dump(), dd(), or even Illuminate\Support\Debug\Dumper can’t peek inside private properties — especially in legacy code or deeply nested service objects.

Implementation Patterns

  • Testing Internal State Safely: Replace brittle @access annotations or reflection boilerplate in PHPUnit tests. For example, assert internal state of a repository or domain service without violating encapsulation:
    $order = new Order($items);
    $reflector = new ObjectReflector();
    $this->assertSame(OrderStatus::CONFIRMED, $reflector->getProperties($order)['status']);
    
  • Enhanced __toString() for Logging: Implement clean debug output in domain models:
    public function __toString(): string
    {
        $reflector = new ObjectReflector();
        $props = $reflector->getProperties($this);
        return get_class($this) . '(' . implode(', ', array_map(
            fn($k, $v) => "$k=" . (is_object($v) ? get_class($v) : $v),
            array_keys($props), $props
        )) . ')';
    }
    
  • Custom Dump Helper: Wrap in a helper to make it reusable across test suites:
    function dump_private(object $object): void
    {
        $props = (new ObjectReflector)->getProperties($object);
        dump($props);
    }
    
    Then call dump_private($user) to see all inherited properties — even private ones from UserTrait or parent classes.
  • Inspecting Eloquent Relationships: For edge cases where relationships leak internal state (e.g., lazy-load state, relations not yet initialized), verify hydration behavior without adding public getters.

Gotchas and Tips

  • PHP Version Alignment is Critical: This package uses strict version support (e.g., v6+ drops PHP 8.3, v5+ drops 8.2). Always run composer show sebastian/object-reflector and compare to php -v. Out-of-sync versions cause silent failures or BC breaks in tests.
  • Naming Collisions Handling: Properties from parent classes appear as ParentClass::property. This avoids ambiguity but breaks naive string matching — e.g., User::id vs Model::id. Use explode('::', $key) to extract class and property parts.
  • Read-Only Properties Are Supported but Immutable: Works on PHP 8.1+ readonly properties, but cannot modify them. Don’t attempt to use ReflectionProperty::setValue() — this package only reads.
  • Avoid Production Use: Reflection has measurable overhead. Enable only in APP_ENV=testing or APP_DEBUG=true. Add a guard:
    if (app()->environment('local', 'testing')) {
        // Use ObjectReflector
    }
    
  • Inheritance Traversal: getProperties() returns all properties across the entire chain — including private ones hidden in deeply nested traits or abstract parents. This is powerful for debugging but may surprise if you expect only declared properties.
  • Final Class, No Extension Points: The core class is final, so no subclassing. Build your own abstraction (e.g., App\Support\PropertyInspector) to insulate app code from direct dependency use.
  • Type Safety Warning: Values are returned as-is — if a property holds a closure or resource, print_r() may fail or expose sensitive data. Sanitize before logging.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport