@@ -29,46 +29,48 @@ Usage
29
29
30
30
### Initialization
31
31
32
- Prior to first use library can be optionally initialized.
33
-
34
- Following example uses the Composer autoloader from the project, where library is used, as a dependency:
35
-
36
- ``` php
37
- $locator = new \Go\ParserReflection\Locator\ComposerLocator();
38
- \Go\ParserReflection\ReflectionEngine::init($locator);
39
- ```
40
-
41
- Following example uses Composer's autoloader located at a given path:
42
-
43
- ``` php
44
- $composerLoader = require_once '/path/to/vendor/autoload.php';
45
- $composerLoader->unregister();
46
-
47
- $locator = new \Go\ParserReflection\Locator\ComposerLocator($composerLoader);
48
- \Go\ParserReflection\ReflectionEngine::init($locator);
49
- ```
32
+ Prior to the first use library can be optionally initialized. If you use Composer for installing packages and loading classes,
33
+ then you shouldn't worry about initialization, library will be initialized automatically.
50
34
51
35
If project uses a custom autoloader then you should follow the next steps:
52
36
53
- 1 . Create a class, that implements ` \Go\ParserReflection\LocatorInterface `
37
+ 1 . Create a new class that implements ` \Go\ParserReflection\LocatorInterface `
54
38
2 . Create an instance of that class and pass it to the ` ReflectionEngine::init() ` method for initial configuration
55
39
56
- ### Actual Usage
40
+ ### Reflecting concrete classes/methods/properties without loading them
57
41
58
42
Just use ` Go\ParserReflection ` package reflection classes like traditional ones:
59
43
60
44
``` php
61
45
$parsedClass = new \Go\ParserReflection\ReflectionClass(SomeClass::class);
62
46
var_dump($parsedClass->getMethods());
47
+
48
+ $parsedMethod = new \Go\ParserReflection\ReflectionMethod(SomeClass::class, 'someMethod');
49
+ echo (string)$parsedMethod;
63
50
```
64
51
65
52
Or you can use an additional classes [ ` ReflectionFile ` ] [ 0 ] and [ ` ReflectionFileNamespace ` ] [ 1 ] to analyse a raw PHP files:
66
53
67
54
``` php
68
55
$parsedFile = new \Go\ParserReflection\ReflectionFile('SomeClass.php');
69
56
$fileNameSpaces = $parsedFile->getFileNamespaces();
70
- var_dump($fileNameSpaces);
71
- var_dump($fileNameSpaces[0]->getClass(SomeClass::class)->getMethods());
57
+ // We can iterate over namespaces in the file
58
+ foreach ($fileNameSpaces as $namespace) {
59
+ $classes = $namespace->getClasses();
60
+ // Iterate over the classes in the namespace
61
+ foreach ($classes as $class) {
62
+ echo "Found class: ", $class->getName(), PHP_EOL;
63
+ // Now let's show all methods in the class
64
+ foreach ($class->getMethods() as $method) {
65
+ echo "Found class method: ", $class->getName(), '::', $method->getName(), PHP_EOL;
66
+ }
67
+
68
+ // ...all properties in the class
69
+ foreach ($class->getProperties() as $property) {
70
+ echo "Found class property: ", $class->getName(), '->', $property->getName(), PHP_EOL;
71
+ }
72
+ }
73
+ }
72
74
```
73
75
74
76
How it works?
0 commit comments