X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=doc%2Fautoloader.md;h=954c28813c870593834b54761222d3ef19aac688;hb=13b184e2b491a3bf916f55e8f148023d82e05d51;hp=5b894cb1a013f9286a9c26671715ea73be4057ed;hpb=e884090efa4d0934a02e6844c97bbf54cdcc9c13;p=friendica.git diff --git a/doc/autoloader.md b/doc/autoloader.md index 5b894cb1a0..954c28813c 100644 --- a/doc/autoloader.md +++ b/doc/autoloader.md @@ -6,7 +6,7 @@ Autoloader with Composer 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) @@ -38,7 +38,7 @@ 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) 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: @@ -46,8 +46,10 @@ The code will be something like: // mod/network.php getAll(); // pass $items to template @@ -58,7 +60,8 @@ function network_content(App $a) { 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 @@ -102,7 +105,7 @@ class Dfrn { mail_post($a){ ... - Friendica\dfrn::mail($item, $owner); + Friendica\Protocol\DFRN::mail($item, $owner); ... } ``` @@ -115,6 +118,8 @@ If your code is in same namespace as the class you need, you don't need to prepe 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 @@ -123,12 +128,12 @@ switch($contact['network']) { 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); [...] ``` @@ -194,4 +199,4 @@ So you can think of namespaces as folders in a Unix file system, with global sco ## 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)