PHPackages                             jasny/reflection-factory - 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. jasny/reflection-factory

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

jasny/reflection-factory
========================

Abstract factory for PHP Reflection

v1.1.2(4y ago)25.5k↓85.9%3MITPHPPHP &gt;=7.4.0

Since Aug 16Pushed 4y agoCompare

[ Source](https://github.com/jasny/reflection-factory)[ Packagist](https://packagist.org/packages/jasny/reflection-factory)[ RSS](/packages/jasny-reflection-factory/feed)WikiDiscussions master Synced 2d ago

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

Jasny Reflection factory
========================

[](#jasny-reflection-factory)

[![PHP](https://github.com/jasny/reflection-factory/actions/workflows/php.yml/badge.svg)](https://github.com/jasny/reflection-factory/actions/workflows/php.yml)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/0904d437bc5c8d903bdfeb0e75de65840fb455663cdf6c6eb3d0d5526b8295d2/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f7265666c656374696f6e2d666163746f72792f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/reflection-factory/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/9741417ff5265801a428b9806ed9cfc29573c7cb10d6ebba01d0a28c16944a7b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f7265666c656374696f6e2d666163746f72792f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/reflection-factory/?branch=master)[![Packagist Stable Version](https://camo.githubusercontent.com/9ec7eccd2f2789d4dcf05ad5951c76d8fc01a9003b5372aa69e6a1771958812a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a61736e792f7265666c656374696f6e2d666163746f72792e737667)](https://packagist.org/packages/jasny/reflection-factory)[![Packagist License](https://camo.githubusercontent.com/d158b183624828afe3cf5e0ff9aabb2ae040b7f0b306b1c7c50589662f3a0f7e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a61736e792f7265666c656374696f6e2d666163746f72792e737667)](https://packagist.org/packages/jasny/reflection-factory)

Factory to use in dependency injection when using [PHP Reflection](https://php.net/reflection).

#### Why use a factory instead or just doing `new ReflectionClass()`?

[](#why-use-a-factory-instead-or-just-doing-new-reflectionclass)

*Dependency injection might seem like making things needlessly complex. However it's a key component in building maintainable (and testable) code.*

Using `new` within your classes creates a strong coupling between classes. This makes it more difficult when writing unit tests because there is no opertunity to mock the reflection. In practice this means that a class using `ReflectionClass` can only be tested with real, existing classes.

Using `ReflectionFactory` with dependency injection, allows you to inject a mock of the factory instead. This in term allows you to create mock Reflection objects, for non-existing classes, functions, properties, etc.

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

[](#installation)

```
composer require jasny/reflection-factory

```

Usage
-----

[](#usage)

```
use Jasny\ReflectionFactory\ReflectionFactory;

$factory = new ReflectionFactory();
$reflection = $factory->reflectClass(\DateTime::class);
```

Example use case
----------------

[](#example-use-case)

#### Without dependency injection

[](#without-dependency-injection)

```
use Jasny\ReflectionFactory\ReflectionFactory;

class SomeTool
{
    public function foo(string $class)
    {
        $reflection = new ReflectionClass($class);

        return $reflection->getConstant('FOO');
    }
}
```

But writing the test is hard, as it doesn't allow mocking. Instead we need to create a `SomeToolTestFooSupport` class just to test this feature.

```
class SomeToolTestFooSupport
{
    const FOO = 10;
}
```

In the unit test we do

```
use PHPUnit\Framework\TestCase;

class SomeToolTest extends TestCase
{
    public function testFoo()
    {
        $tool = new SomeTool();

        $this->assertEquals(10, $tool->foo("SomeToolTestFooSupport"));
    }
}
```

Adding one test class isn't so bad. But consider we need to add one per test, it quickly becomes a mess.

### With dependency injection

[](#with-dependency-injection)

Dependency injection adds a little overhead to the class as we need to pass the reflection factory to `SomeTool`.

```
use Jasny\ReflectionFactory\ReflectionFactoryInterface;

class SomeTool
{
    protected $reflectionFactory;

    public function __construct(ReflectionFactoryInterface $reflectionFactory)
    {
        $this->reflectionFactory = $reflectionFactory;
    }

    public function foo(string $class)
    {
        return $this->reflectionFactory->reflectClass($class)->getConstant('FOO');
    }
}
```

In the unit test, we mock the `ReflectionClass` and `ReflectionFactory`. The tests class `FakeClass` doesn't need to exist.

```
use PHPUnit\Framework\TestCase;
use Jasny\ReflectionFactory\ReflectionFactoryInterface;

class SomeToolTest extends TestCase
{
    public function testFoo()
    {
        $mockReflection = $this->createMock(\ReflectionClass::class);
        $mockReflection->expects($this->once())->method('getConstant')
            ->with('FOO')->willReturn(10);

        $mockFactory = $this->createMock(ReflectionFactoryInterface::class);
        $mockFactory->expects($this->once())->method('reflectClass')
            ->with('FakeClass')->willReturn($mockReflection);

        $tool = new SomeTool($mockFactory);

        $this->assertEquals(10, $tool->foo("FakeClass"));
    }
}
```

### Methods

[](#methods)

MethodReflection class`reflectClass``ReflectionClass``reflectClassConstant``ReflectionClassConstant``reflectZendExtension``ReflectionZendExtension``reflectExtension``ReflectionExtension``reflectFunction``ReflectionFunction``reflectMethod``ReflectionMethod``reflectObject``ReflectionObject``reflectParameter``ReflectionParameter``reflectProperty``ReflectionProperty``reflectGenerator``ReflectionGenerator`Some PHP functions have been wrapped, so they can be mocked

MethodFunction`functionExists`[`function_exists`](https://php.net/function_exists)`classExists`[`class_exists`](https://php.net/class_exists)`methodExists`[`method_exists`](https://php.net/method_exists)`propertyExists`[`property_exists`](https://php.net/property_exists)`extensionLoaded`[`extension_loaded`](https://php.net/extension_loaded)`isA`[`is_a`](https://php.net/is_a)`isCallable`[`is_callable`](https://php.net/is_callable)

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity66

Established project with proven stability

 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

Every ~464 days

Total

4

Last Release

1484d ago

PHP version history (3 changes)v1.0.0PHP &gt;=7.1.0

v1.1.1PHP &gt;=7.2.0

v1.1.2PHP &gt;=7.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3379a93d51305df325df9045e1a8b205d195e4e8c01312dff53a000ee79002eb?d=identicon)[jasny](/maintainers/jasny)

---

Top Contributors

[![jasny](https://avatars.githubusercontent.com/u/100821?v=4)](https://github.com/jasny "jasny (17 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jasny-reflection-factory/health.svg)

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

PHPackages © 2026

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