File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # Dealing with relations
2+
3+ When have a class, you would like to know what is it's base class,
4+ but you could also want to know, what classes extends it.
5+
6+ To deal with that kind of notion php-reflection has a typed bi-directional
7+ reference system.
8+
9+ Lets consider this code :
10+
11+ ``` php
12+ <?php
13+ // from foo.php
14+ abstract class Foo {
15+ abstract protected function doFoo();
16+ }
17+ ```
18+
19+ And into another file :
20+
21+ ``` php
22+ <?php
23+ // from bar.php
24+ class Bar extends Foo {
25+ protected function doFoo() {
26+ return $this;
27+ }
28+ }
29+ ```
30+
31+ ## Preparing relations
32+
33+ Lets say you have made a scan on the workspace :
34+
35+ ``` js
36+ var repository = require (' php-reflection' );
37+ var workspace = new repository (__dirname );
38+ workspace .scan ().then (function () {
39+ // everything is ready
40+ });
41+ ```
42+
43+ So at this point you can make a request to retrieve the Foo class for example :
44+
45+ ``` js
46+ // ...
47+ var fooClass = workspace .getFirstByName (' class' , ' Bar' );
48+ // ...
49+ ```
50+
51+ Each [ :link : node] ( ../src/NODE.md ) has a ` relations ` property, that contains a list of [ :link : relations] ( ../src/RELATION.md ) .
52+
53+ In order to simplify requesting, you can also use the ` getByRelationType ` method, by passing the relation type you want to retrieve.
54+
55+ > Note that this method returns a direct list of [ :link : node] ( ../src/NODE.md ) .
56+
57+ So in principle if I want to retrieve the list of classes that
58+
59+ ``` js
60+ // ...
61+ var fooClass = workspace .getFirstByName (' class' , ' Bar' );
62+ fooClass .getByRelationType (' extends' ).forEach (function (def ) {
63+ console .log (' Extended by ' )
64+ });
65+ // ...
66+ ```
67+
You can’t perform that action at this time.
0 commit comments