Friendica uses [Composer](https://getcomposer.org) to manage dependencies libraries and the class autoloader both for libraries and namespaced Friendica classes.
-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`.
+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.
* [Using Composer](help/Composer)
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)
Let's say now that you need to load some items in a view, maybe in a fictional `mod/network.php`.
-In order for the Composer autoloader to work, it must first be included. In Friendica this is already done at the top of `boot.php`, with `require_once('vendor/autoload.php');`.
+In order for the Composer autoloader to work, it must first be included.
The code will be something like:
// mod/network.php
<?php
+use Friendica\App;
+
function network_content(App $a) {
- $itemsmanager = new Friendica\ItemsManager();
+ $itemsmanager = new \Friendica\ItemsManager();
$items = $itemsmanager->getAll();
// pass $items to template
That's a quite simple example, but look: no `require()`!
If you need to use a class, you can simply use it and you don't need to do anything else.
-Going further: now we have a bunch of `*Manager` classes that cause some code duplication, let's define a `BaseManager` class, where we move all common code between all managers:
+Going further: now we have a bunch of `*Manager` classes that cause some code duplication.
+Let's define a `BaseManager` class, where we move all common code between all managers:
```php
// src/BaseManager.php
mail_post($a){
...
- Friendica\dfrn::mail($item, $owner);
+ Friendica\Protocol\DFRN::mail($item, $owner);
...
}
```
namespace Friendica;
+use Friendica\Protocol\DFRN;
+
// this is the same content of current include/delivery.php,
// but has been declared to be in "Friendica" namespace
case NETWORK_DFRN:
if ($mail) {
$item['body'] = ...
- $atom = Dfrn::mail($item, $owner);
+ $atom = DFRN::mail($item, $owner);
} elseif ($fsuggest) {
- $atom = Dfrn::fsuggest($item, $owner);
+ $atom = DFRN::fsuggest($item, $owner);
q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1", intval($item['id']));
} elseif ($relocate)
- $atom = Dfrn::relocate($owner, $uid);
+ $atom = DFRN::relocate($owner, $uid);
[...]
```
## Related
* [Using Composer](help/Composer)
-* [How To Move Classes to `src`](help/Developer-How-To-Move-Classes-to-src)
\ No newline at end of file
+* [How To Move Classes to `src`](help/Developer-How-To-Move-Classes-to-src)