X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=doc%2Fautoloader.md;h=954c28813c870593834b54761222d3ef19aac688;hb=d26b0ed5a2b5cd66954816096ba87c05043826f0;hp=ea1a82b3572dbbfa4f84a2dc62e046b7fdb7abc0;hpb=45c4e3455e11b2b0ef20fbc53199721951247696;p=friendica.git diff --git a/doc/autoloader.md b/doc/autoloader.md index ea1a82b357..954c28813c 100644 --- a/doc/autoloader.md +++ b/doc/autoloader.md @@ -1,17 +1,202 @@ -Autoloader -========== - -* [Home](help) - -There is some initial support to class autoloading in Friendica core. - -The autoloader code is in `include/autoloader.php`. -It's derived from composer autoloader code. - -Namespaces and Classes are mapped to folders and files in `library/`, -and the map must be updated by hand, because we don't use composer yet. -The mapping is defined by files in `include/autoloader/` folder. - -Currently, only HTMLPurifier library is loaded using autoloader. - - +Autoloader with Composer +========== + +* [Home](help) + * [Developer Intro](help/Developers-Intro) + +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. + +* [Using Composer](help/Composer) + +## A quick introduction to class autoloading + +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". + +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`) + +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). + +### Example + +Let's say you have a PHP file in `src/` that define a very useful class: + +```php +// src/ItemsManager.php +getAll(); + + // pass $items to template + // return result +} +``` + +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: + +```php +// src/BaseManager.php +