bramus / reflection
Better Reflection for PHP
Fund package maintenance!
bramus
Requires
- php: ^7.2
- phpdocumentor/reflection-docblock: ~4.0
Requires (Dev)
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-11-08 04:52:58 UTC
README
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:
\Bramus\Reflection\ReflectionClass
extends PHP's built-inReflectionClass
.\Bramus\Reflection\ReflectionClassConstant
extends PHP's built-inReflectionClassConstant
.
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 callinggetConstants()
. - Return a
\Bramus\Reflection\ReflectionClassConstant
instance (instead of simple value) when callinggetConstant()
.
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 callinggetDocComment()
- 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 scriptcomposer test
$ composer test
License
bramus/reflection
is released under the MIT public license. See the enclosed LICENSE
for details.