X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=doc%2Fautoloader.md;h=83f1010440694cf0fb2109afcf86a11dedcc38ce;hb=6a7a4591d36cd36f8a785488266df106c0025596;hp=69c62451cd9453f04dc9dbc0ec04aaa2924d2550;hpb=e67133ef5653448003ceac29147cda41a4c9e017;p=friendica.git diff --git a/doc/autoloader.md b/doc/autoloader.md index 69c62451cd..83f1010440 100644 --- a/doc/autoloader.md +++ b/doc/autoloader.md @@ -1,209 +1,192 @@ -Autoloader +Autoloader with Composer ========== * [Home](help) + * [Developer Intro](help/Developers-Intro) -There is some initial support to class autoloading in Friendica core. +Friendica uses [Composer](https://getcomposer.org) to manage dependencies libraries and the class autoloader both for libraries and namespaced Friendica classes. -The autoloader code is in `include/autoloader.php`. -It's derived from composer autoloader code. +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`. -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. +* [Using Composer](help/Composer) -Currently, only HTMLPurifier library is loaded using autoloader. +## 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". -## A quick introdution to class autoloading +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`) -The autoloader it's a way for php to automagically include the file that define a class when the class is first used, without the need to use "require_once" every time. +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). -Once is setup you don't have to use it in any way. You need a class? you use the class. +### Example -At his basic is a function passed to the "spl_autoload_register()" function, which receive as argument the class name the script want and is it job to include the correct php file where that class is defined. -The best source for documentation is [php site](http://php.net/manual/en/language.oop5.autoload.php). +Let's say you have a PHP file in `src/` that define a very useful class: -One example, based on fictional friendica code. +```php + // src/ItemsManager.php + array($baseDir."/include"); - ); + class ItemsManager { + public function getAll() { ... } + public function getByID($id) { ... } + } ``` +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) -That tells the autoloader code to look for files that defines classes in "Friendica" namespace under "include/" folder. (And btw, that's why the file has the same name as the class it defines.) - -*note*: The structure of files in "include/autoloader/" has been copied from the code generated by composer, to ease the work of enable autoloader for external libraries under "library/" - -Let's say now that you need to load some items in a view, maybe in a fictional "mod/network.php". -Somewere at the start of the scripts, the autoloader was initialized. In Friendica is done at the top of "boot.php", with "require_once('include/autoloader.php');". +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');`. The code will be something like: -``` - file: mod/network.php - getAll(); + function network_content(App $a) { + $itemsmanager = new \Friendica\ItemsManager(); + $items = $itemsmanager->getAll(); - // pass $items to template - // return result - } + // pass $items to template + // return result + } ``` -That's a quite simple example, but look: no "require()"! -You need to use a class, you use the class and you don't need to do anything more. +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 to move all code in common 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: -``` - file: include/BaseManager.php -