PHPackages                             bentools/reflection-plus - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. bentools/reflection-plus

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

bentools/reflection-plus
========================

Another improved code reflection API

1.0(1y ago)11.1k↓92.9%1MITPHPPHP &gt;=8.4CI passing

Since May 20Pushed 1y ago1 watchersCompare

[ Source](https://github.com/bpolaszek/reflection-plus)[ Packagist](https://packagist.org/packages/bentools/reflection-plus)[ RSS](/packages/bentools-reflection-plus/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (1)Dependencies (5)Versions (2)Used By (1)

Reflection Plus!
================

[](#reflection-plus)

[![Latest Version on Packagist](https://camo.githubusercontent.com/1b9d2e1e825fec20777578a0c857bc74c3473c3554d1d8c5ef9466d37eed2153/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f62656e746f6f6c732f7265666c656374696f6e2d706c75732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bentools/reflection-plus)[![Total Downloads](https://camo.githubusercontent.com/59ad73b79c8c62a2418f2b5c369c1ff44ba5905f4d098ddb88322c9a5e95b173/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f62656e746f6f6c732f7265666c656374696f6e2d706c75732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bentools/reflection-plus)[![Tests](https://camo.githubusercontent.com/9d0218b22fd619bdb18ce43c3847910e9ae936a565831625950f2bde07a7cee9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f62706f6c61737a656b2f7265666c656374696f6e2d706c75732f63692e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/bentools/reflection-plus/actions/workflows/run-tests.yml)[![Coverage](https://camo.githubusercontent.com/d2814ba7b05ec992636d5b0f0087069ae2803d4f31eca4604cb1368ccc9251c4/68747470733a2f2f636f6465636f762e696f2f67682f62706f6c61737a656b2f7265666c656374696f6e2d706c75732f67726170682f62616467652e7376673f746f6b656e3d4143763155776c716674)](https://codecov.io/gh/bpolaszek/reflection-plus)

**ReflectionPlus** is a lightweight wrapper around PHP's native Reflection API that makes working with reflection simpler, more efficient, and more intuitive.

Features
--------

[](#features)

- **Cache-enabled reflection**: Automatically caches reflection instances for better performance
- **Simplified API**: Clean, intuitive methods for accessing reflection information
- **Type compatibility analysis**: Easily determine if classes are compatible with property types
- **Support for complex type systems**: Full handling of union types, intersection types, and named types
- **Performance optimized**: Uses WeakMap and other optimizations to minimize memory usage

Why?
----

[](#why)

ReflectionPlus automatically caches:

- ReflectionClass instances
- ReflectionProperty instances
- ReflectionMethod instances
- Type compatibility results

This makes it highly efficient for repeated usage, especially in loops or recursive operations.

Use Cases
---------

[](#use-cases)

ReflectionPlus is particularly useful for:

- Factory implementations that need to determine which concrete classes to instantiate
- Dependency injection containers
- Type-based serialization/deserialization systems
- Code generators that need to inspect class structures
- Data mappers that need to determine type compatibility
- Framework development where reflection is frequently used

Requirements
------------

[](#requirements)

- PHP 8.4 or higher

Usage
-----

[](#usage)

### Basic Reflection Operations

[](#basic-reflection-operations)

Get a reflection class:

```
use BenTools\ReflectionPlus\Reflection;

// From a class name
$reflectionClass = Reflection::class(MyClass::class);

// Or from an object
$object = new MyClass();
$reflectionClass = Reflection::class($object);
```

Get a reflection property:

```
// From a class name
$reflectionProperty = Reflection::property(MyClass::class, 'someProperty');

// Or from an object
$object = new MyClass();
$reflectionProperty = Reflection::property($object, 'someProperty');
```

Get a reflection method:

```
// From a class name
$reflectionMethod = Reflection::method(MyClass::class, 'someMethod');

// Or from an object
$object = new MyClass();
$reflectionMethod = Reflection::method($object, 'someMethod');
```

### Working with Property Types

[](#working-with-property-types)

Get all instantiable class types that can be set to a property:

```
// Get a reflection property
$reflectionProperty = Reflection::property($myClass, 'myProperty');

// Get all class types that can be set to this property
$classTypes = Reflection::getSettableClassTypes($reflectionProperty);

// Returns an array of fully qualified class names
// that are compatible with the property type
// and are instantiable (no interfaces or abstract classes)
```

Check if a class is compatible with a property:

```
$reflectionProperty = Reflection::property($myClass, 'myProperty');

// Check if a particular class is compatible
$isCompatible = Reflection::isPropertyCompatible($reflectionProperty, SomeClass::class);

// Returns true if SomeClass can be assigned to the property
```

Find the best class for a property from a list of candidates:

```
$reflectionProperty = Reflection::property($myClass, 'myProperty');
$classNames = [ClassA::class, ClassB::class, ClassC::class];

// Find the first compatible class in the array
$bestClass = Reflection::getBestClassForProperty($reflectionProperty, $classNames);

// Returns the class name of the first compatible class
// or throws InvalidArgumentException if none are compatible
```

### Type Compatibility

[](#type-compatibility)

Check type compatibility with different type systems:

```
$reflectionProperty = Reflection::property($myClass, 'myProperty');
$type = $reflectionProperty->getType();

// Check if a class is compatible with this type
$isCompatible = Reflection::isTypeCompatible($type, SomeClass::class);

// Works with:
// - ReflectionNamedType (standard class or primitive types)
// - ReflectionUnionType (Type1|Type2)
// - ReflectionIntersectionType (Type1&Type2)
```

Advanced Use Cases
------------------

[](#advanced-use-cases)

### Working with Union Types

[](#working-with-union-types)

```
// For a property like: public ClassA|ClassB $property;

$reflectionProperty = Reflection::property($class, 'property');
$classTypes = Reflection::getSettableClassTypes($reflectionProperty);

// $classTypes will contain [ClassA::class, ClassB::class] if both are instantiable
```

### Working with Intersection Types

[](#working-with-intersection-types)

```
// For a property like: public ClassA&InterfaceB $property;

$reflectionProperty = Reflection::property($class, 'property');

// You can check if a specific class meets all requirements
$isCompatible = Reflection::isPropertyCompatible($reflectionProperty, SomeClass::class);

// Returns true if SomeClass extends/is ClassA AND implements InterfaceB
```

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require bentools/reflection-plus
```

License
-------

[](#license)

MIT.

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance46

Moderate activity, may be stable

Popularity17

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

410d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/acdd1a8ee0e657ddd06cf11f98a32100ef7121afb8aa270a5b295f5c29c038b3?d=identicon)[bpolaszek](/maintainers/bpolaszek)

---

Top Contributors

[![bpolaszek](https://avatars.githubusercontent.com/u/5569077?v=4)](https://github.com/bpolaszek "bpolaszek (10 commits)")

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/bentools-reflection-plus/health.svg)

```
[![Health](https://phpackages.com/badges/bentools-reflection-plus/health.svg)](https://phpackages.com/packages/bentools-reflection-plus)
```

###  Alternatives

[beyondcode/laravel-vouchers

Allow users to redeem vouchers that are bound to models..

70768.4k2](/packages/beyondcode-laravel-vouchers)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
