bramus/reflection

Better Reflection for PHP

Fund package maintenance!
bramus

1.0 2019-05-13 06:47 UTC

This package is auto-updated.

Last update: 2024-10-08 04:42:01 UTC


README

Build Status Source Version Downloads License

bramus/reflection is a library that tries to make PHP's built-in Reflection better.

Built by Bram(us) Van Damme (https://www.bram.us) and Contributors

Prerequisites/Requirements

  • PHP 7.2 or greater

Installation

Installation is possible using Composer

$ composer require bramus/reflection ~1.0

Usage

A note

All classes in bramus/reflection extend PHP's built-in versions. Therefore they have all of the functions like their parent class:

ReflectionClass

When compared to \ReflectionClass, \Bramus\Reflection\ReflectionClass works exactly the same, but will:

  • Return an associative array containing \Bramus\Reflection\ReflectionClassConstant instances (instead of simple values) when calling getConstants().
  • Return a \Bramus\Reflection\ReflectionClassConstant instance (instead of simple value) when calling getConstant().

Here's an example comparing getConstant();

  • Using PHP's built-in ReflectionClass:

     class Weekday
     {
     	/**
     	 * Monday
     	 */
     	const MONDAY = 1;
    
     	/**
     	 * Tuesday
     	 */
     	const TUESDAY = …
     }
    
     $reflected = new \ReflectionClass(Weekday::class);
     $constant = $reflected->getConstant('MONDAY');
    
     var_dump($constant);
     // int(1)
  • Using \Bramus\Reflection\ReflectionClass:

     class Weekday
     {
     	/**
     	 * Monday
     	 */
     	const MONDAY = 1;
    
     	/**
     	 * Tuesday
     	 */
     	const TUESDAY = …
     }
    
     $reflected = new \Bramus\Reflection\ReflectionClass(Weekday::class);
     $constants = $reflected->getConstant('MONDAY');
    
     var_dump($constant);
     // object(Bramus\Reflection\ReflectionClassConstant)#40 (2) {
     //   ["name"]=>
     //   string(6) "MONDAY"
     //   ["class"]=>
     //   string(7) "Weekday"
     //   ["docComment":"Bramus\Reflection\ReflectionClassConstant":private]=>
     //   object(phpDocumentor\Reflection\DocBlock)#86 (7) {
     //     …
     //   }
     // }

ReflectionClassConstant

When compared to \ReflectionClassConstant, \Bramus\Reflection\ReflectionClassConstant works exactly the same, but will:

  • Return a \phpDocumentor\Reflection\DocBlock instance (instead of a string) when calling getDocComment()
  • Provide you with a getDocCommentString() method in case you want to access the contents as \ReflectionClassConstant::getDocComment() would return
  • Provide you with a getSummary() shorthand, directly on the \Bramus\Reflection\ReflectionClassConstant instance.
  • Provide you with a getDescription() shorthand, directly on the \Bramus\Reflection\ReflectionClassConstant instance.

Other Reflection Classes

Other Reflection Classes are not provided. They might be in the future.

Testing

bramus/reflection ships with unit tests using PHPUnit ~8.0.

  • If PHPUnit is installed globally run phpunit to run the tests.
  • If PHPUnit is not installed globally, install it locally throuh composer by running composer install --dev. Run the tests themselves by calling ./vendor/bin/phpunit or using the composer script composer test
$ composer test

License

bramus/reflection is released under the MIT public license. See the enclosed LICENSE for details.