PHPackages                             sun/staticreflection - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. sun/staticreflection

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

sun/staticreflection
====================

Static PHP class code reflection for post-discovery scenarios.

1.0.0(11y ago)124MITPHPPHP &gt;=5.4.2

Since Jun 26Pushed 11y ago1 watchersCompare

[ Source](https://github.com/sun/staticreflection)[ Packagist](https://packagist.org/packages/sun/staticreflection)[ RSS](/packages/sun-staticreflection/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (1)DependenciesVersions (3)Used By (0)

StaticReflection [![Build Status](https://camo.githubusercontent.com/23a8781053d534bee680a78854db7a4c5ebcef0fe8629ff7e73fe16790118cc1/68747470733a2f2f7472617669732d63692e6f72672f73756e2f7374617469637265666c656374696f6e2e737667)](https://travis-ci.org/sun/staticreflection) [![Code Coverage](https://camo.githubusercontent.com/ce508aec09e0a96cf576ac1a69a98e8130a650a39e7d3dfc7ad21227003173fc/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f73756e2f7374617469637265666c656374696f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/sun/staticreflection/?branch=master) [![Code Quality](https://camo.githubusercontent.com/f3c3cbd53910a96d21380dc7daa89c8021aaa74f0f7e6144410e52c3fcfdc8bf/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f73756e2f7374617469637265666c656374696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/sun/staticreflection/?branch=master)
=======================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#staticreflection---)

*Static PHP class code reflection for post-discovery scenarios.*

This utility library for PHP frameworks allows to reflect the file header of a PHP class without loading its code into memory, if its filesystem location is known already (e.g., via discovery/classmap).

Static reflection is useful to filter a large list of previously discovered class files for common aspects like interfaces or base classes.

`ReflectionClass` provides the same API as the native `\ReflectionClass`.

Native [PHP Reflection](http://php.net/manual/en/book.reflection.php) can easily grow out of control, because it not only loads the reflected class, but also all dependent classes and interfaces. PHP code cannot be unloaded. A high memory consumption may cause the application to exceed PHP's memory limit. — Static reflection avoids to (auto-)load all dependencies and ancestor classes of each reflected class into memory.

In the worst/ideal use-case, you're only generating a list of *available*classes, without using them immediately (e.g., for user selection or swappable plugin implementations).

Example [xhprof](http://php.net/manual/en/book.xhprof.php) diff result:

1,538 candidate classes, of which 180 interfaces, traits, abstract and other helper classes are filtered out:

\\ReflectionClassReflectionClassDiffDiff%Number of Function Calls64,747202,783138,036213.2%Incl. Wall Time (microsecs)2,514,8013,272,539757,73830.1%Incl. CPU (microsecs)2,480,4153,120,020639,60525.8%Incl. MemUse (bytes)108,805,12010,226,160-98,578,960-90.6%Incl. PeakMemUse (bytes)108,927,21610,347,608-98,579,608**-90.5%**Usage Example
-------------

[](#usage-example)

1. Prerequisite: Some discovery produces a classmap:

    ```
    {
      "Sun\StaticReflection\ReflectionClass":
        "./src/ReflectionClass.php",
      "Sun\Tests\StaticReflection\ReflectionClassTest":
        "./tests/src/ReflectionClassTest.php",
      "Sun\Tests\StaticReflection\Fixtures\Example":
        "./tests/fixtures/Example.php",
      "Sun\Tests\StaticReflection\Fixtures\Base\ImportedInterface":
        "./tests/fixtures/Base/ImportedInterface.php"
      ...
    }
    ```

    → You have a `classname => pathname` map.
2. Filter all discovered class files:

    ```
    use Sun\StaticReflection\ReflectionClass;

    $list = array();
    foreach ($classmap as $classname => $pathname) {
      $class = new ReflectionClass($classname, $pathname);

      // Only include tests.
      if (!$class->isSubclassOf('PHPUnit_Framework_TestCase')) {
        continue;
      }

      // …optionally prepare them for a listing/later selection:
      $doc_comment = $class->getDocComment();
      $list[$classname] = array(
        'summary' => $doc_comment->getSummary(),
        'covers' => $doc_comment->getAnnotations()['coversDefaultClass'][0],
      );
    }
    echo json_encode($list, JSON_PRETTY_PRINT);
    ```

    ```
    {
      "Sun\Tests\StaticReflection\ReflectionClassTest": {
        "summary": "Tests ReflectionClass.",
        "covers": "\Sun\StaticReflection\ReflectionClass"
      }
    }
    ```

    → You filtered the list of available classes, without loading all code into memory.
3. Why this matters:

    ```
    array_walk($classmap, function (&$pathname, $classname) {
      $pathname = class_exists($classname, FALSE) || interface_exists($classname, FALSE);
    });
    echo json_encode($classmap, JSON_PRETTY_PRINT);
    ```

    ```
    {
      "Sun\Tests\StaticReflection\ReflectionClassTest": false,
      "Sun\Tests\StaticReflection\Fixtures\Example": false,
      "Sun\Tests\StaticReflection\Fixtures\ExampleInterface": true,
      "Sun\Tests\StaticReflection\Fixtures\Base\Example": true,
      ...
    }
    ```

    → Only the **ancestors** of each class/interface were loaded. The statically reflected classes themselves did not get loaded.
4. *ProTip™* - `ReflectionClass::isSubclassOfAny()`

    To filter for a set of common parent classes/interfaces, check the statically reflected information first. Only proceed to `isSubclassOf()` in case you need to check further; e.g.:

    ```
    // Static reflection.
    if (!$class->isSubclassOfAny(array('Condition\FirstFlavor', 'Condition\SecondFlavor'))) {
      continue;
    }
    // Native reflection of ancestors (if the reflected class has any).
    if (!$class->isSubclassOf('Condition\BaseFlavor')) {
      continue;
    }
    ```

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

[](#requirements)

- PHP 5.4.2+

Limitations
-----------

[](#limitations)

1. Only one class/interface/trait per file (PSR-2, PSR-0/PSR-4), which must be defined *first* in the file.
2. `implementsInterface($interface)` returns `TRUE` even if `$interface` is a class.
3. `\ReflectionClass::IS_IMPLICIT_ABSTRACT` is not supported, since methods are not analyzed. (only the file header is analyzed)
4. `\ReflectionClass::$name` is read-only and thus not available. Use `getName()` instead.
5. Calling any other `\ReflectionClass` methods that are not implemented (yet) causes a fatal error.

    The parent `\ReflectionClass` class might be lazily instantiated on-demand in the future (PRs welcome). `ReflectionClass` does implement all methods that can be technically supported already.

Notes
-----

[](#notes)

- StaticReflection may work around bytecode caches that strip off comments.

Inspirations
------------

[](#inspirations)

Static/Reflection:

- Doctrine's (Static) [Reflection](https://github.com/doctrine/common/tree/master/lib/Doctrine/Common/Reflection)
- phpDocumentor's [Reflection](https://github.com/phpDocumentor/Reflection)
- Zend Framework's [Reflection](https://github.com/zendframework/zf2/tree/master/library/Zend/Server/Reflection)

PHPDoc tags/annotations:

- PHPUnit's [Util\\Test](https://github.com/sebastianbergmann/phpunit/blob/master/src/Util/Test.php)
- Doctrine's [Annotations](https://github.com/doctrine/annotations/tree/master/lib/Doctrine/Common/Annotations)
- phpDocumentor's [ReflectionDocBlock](https://github.com/phpDocumentor/ReflectionDocBlock) + [Descriptor](https://github.com/phpDocumentor/phpDocumentor2/tree/develop/src/phpDocumentor/Descriptor)
- Kuria's [PhpDocComment](https://github.com/kuria/php-doc-comment)
- Philip Graham's [Annotations](https://github.com/pgraham/php-annotations)

License
-------

[](#license)

[MIT](LICENSE) — Copyright (c) 2014 Daniel F. Kudwien (sun)

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

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

4344d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/71ec59a80244af1aee11151b4897af426b5f752173d2e566ba69060fb18c0319?d=identicon)[sun](/maintainers/sun)

---

Top Contributors

[![sun](https://avatars.githubusercontent.com/u/41992?v=4)](https://github.com/sun "sun (56 commits)")

---

Tags

phpdocparserautoloadreflectionannotationsdocblocktokendiscovery

### Embed Badge

![Health badge](/badges/sun-staticreflection/health.svg)

```
[![Health](https://phpackages.com/badges/sun-staticreflection/health.svg)](https://phpackages.com/packages/sun-staticreflection)
```

###  Alternatives

[doctrine/lexer

PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.

11.2k910.8M118](/packages/doctrine-lexer)[jan-swiecki/simple-annotations

Simple annotation parser

66615.0k18](/packages/jan-swiecki-simple-annotations)[phpowermove/docblock

PHP Docblock parser and generator. An API to read and write Docblocks.

2524.0M4](/packages/phpowermove-docblock)[type-lang/parser

Library for parsing and validating TypeLang syntax and converting it into AST nodes

5158.4k6](/packages/type-lang-parser)[sbsaga/toon

🧠 TOON for Laravel — a compact, human-readable, and token-efficient data format for AI prompts &amp; LLM contexts. Perfect for ChatGPT, Gemini, Claude, Mistral, and OpenAI integrations (JSON ⇄ TOON).

6115.6k](/packages/sbsaga-toon)[dannykopping/docblock

A simple library for parsing PHP DocBlock comments

191.8k2](/packages/dannykopping-docblock)

PHPackages © 2026

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