friendica_2021.01_tupambae_.../doc/autoloader.md

193 lines
5.7 KiB
Markdown
Raw Normal View History

2017-04-01 23:58:21 +02:00
Autoloader with Composer
==========
* [Home](help)
2017-04-01 23:58:21 +02:00
* [Developer Intro](help/Developers-Intro)
2017-04-01 23:58:21 +02:00
Friendica uses [Composer](https://getcomposer.org) to manage dependencies libraries and the class autoloader both for libraries and namespaced Friendica classes.
2017-04-01 23:58:21 +02:00
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`.
2017-04-01 23:58:21 +02:00
* [Using Composer](help/Composer)
2017-04-01 23:58:21 +02:00
## A quick introduction to class autoloading
2017-04-01 23:58:21 +02:00
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".
2017-04-01 23:58:21 +02:00
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`)
2016-02-13 10:57:37 +01:00
2017-04-01 23:58:21 +02:00
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.
For more info about PHP autoloading, please refer to the [official PHP documentation](http://php.net/manual/en/language.oop5.autoload.php).
2016-02-13 10:57:37 +01:00
2017-04-01 23:58:21 +02:00
### Example
2016-02-13 10:57:37 +01:00
2017-04-01 23:58:21 +02:00
Let's say you have a PHP file in `src/` that define a very useful class:
2016-02-13 10:57:37 +01:00
2017-04-01 23:58:21 +02:00
```php
// src/ItemsManager.php
<?php
namespace \Friendica;
2016-02-13 10:57:37 +01:00
2017-04-01 23:58:21 +02:00
class ItemsManager {
public function getAll() { ... }
public function getByID($id) { ... }
}
2016-02-13 10:57:37 +01:00
```
2017-04-01 23:58:21 +02:00
The class `ItemsManager` has been declared in the `Friendica` namespace.
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)
2016-02-13 10:57:37 +01:00
2017-04-01 23:58:21 +02:00
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');`.
2016-02-13 10:57:37 +01:00
The code will be something like:
2017-04-01 23:58:21 +02:00
```php
// mod/network.php
<?php
2017-04-01 23:58:21 +02:00
function network_content(App $a) {
$itemsmanager = new \Friendica\ItemsManager();
$items = $itemsmanager->getAll();
2017-04-01 23:58:21 +02:00
// pass $items to template
// return result
}
2016-02-13 10:57:37 +01:00
```
2017-04-01 23:58:21 +02:00
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.
2016-02-13 10:57:37 +01:00
2017-04-01 23:58:21 +02:00
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:
2016-02-13 10:57:37 +01:00
2017-04-01 23:58:21 +02:00
```php
// src/BaseManager.php
<?php
namespace \Friendica;
2017-04-01 23:58:21 +02:00
class BaseManager {
public function thatFunctionEveryManagerUses() { ... }
}
2016-02-13 10:57:37 +01:00
```
and then let's change the ItemsManager class to use this code
2017-04-01 23:58:21 +02:00
```php
// src/ItemsManager.php
<?php
namespace \Friendica;
2017-04-01 23:58:21 +02:00
class ItemsManager extends BaseManager {
public function getAll() { ... }
public function getByID($id) { ... }
}
2016-02-13 10:57:37 +01:00
```
2017-04-01 23:58:21 +02:00
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.
It works with the "BaseManager" example here and it works when we need to call static methods:
2016-02-13 10:57:37 +01:00
2017-04-01 23:58:21 +02:00
```php
// src/Dfrn.php
<?php
namespace \Friendica;
2017-04-01 23:58:21 +02:00
class Dfrn {
public static function mail($item, $owner) { ... }
}
2016-02-13 10:57:37 +01:00
```
2017-04-01 23:58:21 +02:00
```php
// mod/mail.php
<?php
2017-04-01 23:58:21 +02:00
mail_post($a){
...
\Friendica\dfrn::mail($item, $owner);
...
}
2016-02-13 10:57:37 +01:00
```
If your code is in same namespace as the class you need, you don't need to prepend it:
2017-04-01 23:58:21 +02:00
```php
// include/delivery.php
<?php
2017-04-01 23:58:21 +02:00
namespace \Friendica;
2017-04-01 23:58:21 +02:00
// this is the same content of current include/delivery.php,
// but has been declared to be in "Friendica" namespace
2017-04-01 23:58:21 +02:00
[...]
switch($contact['network']) {
case NETWORK_DFRN:
if ($mail) {
$item['body'] = ...
$atom = Dfrn::mail($item, $owner);
} elseif ($fsuggest) {
$atom = Dfrn::fsuggest($item, $owner);
q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1", intval($item['id']));
} elseif ($relocate)
$atom = Dfrn::relocate($owner, $uid);
[...]
2016-02-13 10:57:37 +01:00
```
2017-04-01 23:58:21 +02:00
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.
2016-02-13 10:57:37 +01:00
But if you want to use classes from another library, you need to use the full namespace, e.g.
2017-04-01 23:58:21 +02:00
```php
// src/Diaspora.php
<?php
2017-04-01 23:58:21 +02:00
namespace \Friendica;
class Diaspora {
public function md2bbcode() {
$html = \Michelf\MarkdownExtra::defaultTransform($text);
}
}
2016-02-13 10:57:37 +01:00
```
2017-04-01 23:58:21 +02:00
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
2016-02-13 10:57:37 +01:00
2017-04-01 23:58:21 +02:00
```php
// src/Diaspora.php
<?php
namespace \Friendica;
2017-04-01 23:58:21 +02:00
use \Michelf\MarkdownExtra;
2017-04-01 23:58:21 +02:00
class Diaspora {
public function md2bbcode() {
$html = MarkdownExtra::defaultTransform($text);
}
}
2016-02-13 10:57:37 +01:00
```
Note that namespaces are like paths in filesystem, separated by "\", with the first "\" being the global scope.
2017-04-01 23:58:21 +02:00
You can go deeper if you want to, like:
2016-02-13 10:57:37 +01:00
```
2017-04-01 23:58:21 +02:00
// src/Network/Dfrn.php
2016-02-13 10:57:37 +01:00
<?php
namespace \Friendica\Network;
2017-04-01 23:58:21 +02:00
class Dfrn {
2016-02-13 10:57:37 +01:00
}
```
2017-04-01 23:58:21 +02:00
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`.
2016-02-13 10:57:37 +01:00
or
```
2017-04-01 23:58:21 +02:00
// src/Dba/Mysql
2016-02-13 10:57:37 +01:00
<?php
2017-04-01 23:58:21 +02:00
namespace \Friendica\Dba;
2017-04-01 23:58:21 +02:00
class Mysql {
2016-02-13 10:57:37 +01:00
}
```
2017-04-01 23:58:21 +02:00
So you can think of namespaces as folders in a Unix file system, with global scope as the root ("\").