forgotten $
[friendica.git/.git] / doc / autoloader.md
1 Autoloader with Composer
2 ==========
3
4 * [Home](help)
5   * [Developer Intro](help/Developers-Intro)
6
7 Friendica uses [Composer](https://getcomposer.org) to manage dependencies libraries and the class autoloader both for libraries and namespaced Friendica classes.
8
9 It's a command-line tool that downloads required libraries into the `vendor` folder and makes any namespaced class in `src` available through the whole application through `boot.php`.
10
11 * [Using Composer](help/Composer)
12
13 ## A quick introduction to class autoloading
14
15 The autoloader dynamically includes the file defining a class when it is first referenced, either by instantiating an object or simply making sure that it is available, without the need to explicitly use "require_once".
16
17 Once it is set up you don't have to directly use it, you can directly use any class that is covered by the autoloader (currently `vendor` and `src`)
18
19 Under the hood, Composer registers a callback with [`spl_autoload_register()`](http://php.net/manual/en/function.spl-autoload-register.php) that receives a class name as an argument and includes the corresponding class definition file.
20 For more info about PHP autoloading, please refer to the [official PHP documentation](http://php.net/manual/en/language.oop5.autoload.php).
21
22 ### Example
23
24 Let's say you have a PHP file in `src/` that define a very useful class:
25
26 ```php
27 // src/ItemsManager.php
28 <?php
29 namespace Friendica;
30
31 class ItemsManager {
32         public function getAll() { ... }
33         public function getByID($id) { ... }
34 }
35 ```
36
37 The class `ItemsManager` has been declared in the `Friendica` namespace.
38 Namespaces are useful to keep classes separated and avoid names conflicts (could be that a library you want to use also defines a class named `ItemsManager`, but as long as it is in another namespace, you don't have any problem)
39
40 Let's say now that you need to load some items in a view, maybe in a fictional `mod/network.php`.
41 In order for the Composer autoloader to work, it must first be included.
42 In Friendica this is already done at the top of `boot.php`, with `require_once('vendor/autoload.php');`.
43
44 The code will be something like:
45
46 ```php
47 // mod/network.php
48 <?php
49
50 function network_content(App $a) {
51         $itemsmanager = new Friendica\ItemsManager();
52         $items = $itemsmanager->getAll();
53
54         // pass $items to template
55         // return result
56 }
57 ```
58
59 That's a quite simple example, but look: no `require()`!
60 If you need to use a class, you can simply use it and you don't need to do anything else.
61
62 Going further: now we have a bunch of `*Manager` classes that cause some code duplication.
63 Let's define a `BaseManager` class, where we move all common code between all managers:
64
65 ```php
66 // src/BaseManager.php
67 <?php
68 namespace Friendica;
69
70 class BaseManager {
71         public function thatFunctionEveryManagerUses() { ... }
72 }
73 ```
74
75 and then let's change the ItemsManager class to use this code
76
77 ```php
78 // src/ItemsManager.php
79 <?php
80 namespace Friendica;
81
82 class ItemsManager extends BaseManager {
83         public function getAll() { ... }
84         public function getByID($id) { ... }
85 }
86 ```
87
88 Even though we didn't explicitly include the `src/BaseManager.php` file, the autoloader will when this class is first defined, because it is referenced as a parent class.
89 It works with the "BaseManager" example here and it works when we need to call static methods:
90
91 ```php
92 // src/Dfrn.php
93 <?php
94 namespace Friendica;
95
96 class Dfrn {
97         public static function  mail($item, $owner) { ... }
98 }
99 ```
100
101 ```php
102 // mod/mail.php
103 <?php
104
105 mail_post($a){
106         ...
107         Friendica\Protocol\DFRN::mail($item, $owner);
108         ...
109 }
110 ```
111
112 If your code is in same namespace as the class you need, you don't need to prepend it:
113
114 ```php
115 // include/delivery.php
116 <?php
117
118 namespace Friendica;
119
120 // this is the same content of current include/delivery.php,
121 // but has been declared to be in "Friendica" namespace
122
123 [...]
124 switch($contact['network']) {
125         case NETWORK_DFRN:
126                 if ($mail) {
127                         $item['body'] = ...
128                         $atom = DFRN::mail($item, $owner);
129                 } elseif ($fsuggest) {
130                         $atom = DFRN::fsuggest($item, $owner);
131                         q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1", intval($item['id']));
132                 } elseif ($relocate)
133                         $atom = DFRN::relocate($owner, $uid);
134 [...]
135 ```
136
137 This is the current code of `include/delivery.php`, and since the code is declared to be in the "Friendica" namespace, you don't need to write it when you need to use the "Dfrn" class.
138 But if you want to use classes from another library, you need to use the full namespace, e.g.
139
140 ```php
141 // src/Diaspora.php
142 <?php
143
144 namespace Friendica;
145
146 class Diaspora {
147         public function md2bbcode() {
148                 $html = \Michelf\MarkdownExtra::defaultTransform($text);
149         }
150 }
151 ```
152
153 if you use that class in many places of the code and you don't want to write the full path to the class every time, you can use the "use" PHP keyword
154
155 ```php
156 // src/Diaspora.php
157 <?php
158 namespace Friendica;
159
160 use \Michelf\MarkdownExtra;
161
162 class Diaspora {
163         public function md2bbcode() {
164                 $html = MarkdownExtra::defaultTransform($text);
165         }
166 }
167 ```
168
169 Note that namespaces are like paths in filesystem, separated by "\", with the first "\" being the global scope.
170 You can go deeper if you want to, like:
171
172 ```
173 // src/Network/Dfrn.php
174 <?php
175 namespace Friendica\Network;
176
177 class Dfrn {
178 }
179 ```
180
181 Please note that the location of the file defining the class must be placed in the appropriate sub-folders of `src` if the namespace isn't plain `Friendica`.
182
183 or
184
185 ```
186 // src/Dba/Mysql
187 <?php
188 namespace Friendica\Dba;
189
190 class Mysql {
191 }
192 ```
193
194 So you can think of namespaces as folders in a Unix file system, with global scope as the root ("\").
195
196 ## Related
197
198 * [Using Composer](help/Composer)
199 * [How To Move Classes to `src`](help/Developer-How-To-Move-Classes-to-src)