Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
4.7 KiB

  1. How To Move Classes to `src`
  2. ==============
  3. * [Home](help)
  4. * [Developer Intro](help/Developers-Intro)
  5. Friendica uses [Composer](help/Composer) to manage autoloading.
  6. This means that all the PHP class files moved to the `src` folder will be [automatically included](help/autoloader) when the class it defines is first used in the flow.
  7. This is an improvement over the current `require` usage since files will be included on an actual usage basis instead of the presence of a `require` call.
  8. However, there are a significant number of items to check when moving a class file from the `include` folder to the `src` folder, and this page is there to list them.
  9. ## Decide the namespace
  10. This isn't the most technical decision of them all, but it has long lasting consequences as it will be the name that will be used to refer to this class from now on.
  11. There is [a shared Ethercalc sheet](https://ethercalc.org/friendica_classes) to suggest namespace/class names that lists all the already moved class files for inspiration.
  12. A few pointers though:
  13. * `Friendica` is the base namespace for all classes in the `src` folder
  14. * Namespaces match the directory structure, with `Friendica` namespace being the base `src` directory. The `Config` class set in the `Friendica\Core` namespace is expected to be found at `src/Core/Config.php`.
  15. * Namespaces can help group classes with a similar purpose or relevant to a particular feature
  16. When you're done deciding the namespace, it's time to use it.
  17. Let's say we choose `Friendica\Core` for the `Config` class.
  18. ## Use the namespace
  19. To declare the namespace, the file `src/Core/Config.php` must start with the following statement:
  20. ````php
  21. namespace Friendica\Core;
  22. ````
  23. From now on, the `Config` class can be referred to as `Friendica\Core\Config`, however it isn't very practical, especially when the class was previously used as `Config`.
  24. Thankfully, PHP provides namespace shortcuts through `use`.
  25. This language construct just provides a different naming scheme for a namespace or a class, but doesn't trigger the autoload mechanism on its own.
  26. Here are the different ways you can use `use`:
  27. ````php
  28. // No use
  29. $config = new Friendica\Core\Config();
  30. ````
  31. ````php
  32. // Namespace shortcut
  33. use Friendica\Core;
  34. $config = new Core\Config();
  35. ````
  36. ````php
  37. // Class name shortcut
  38. use Friendica\Core\Config;
  39. $config = new Config();
  40. ````
  41. ````php
  42. // Aliasing
  43. use Friendica\Core\Config as Cfg;
  44. $config = new Cfg();
  45. ````
  46. Whatever the style chosen, a repository-wide search has to be done to find all the class name usage and either use the fully-qualified class name (including the namespace) or add a `use` statement at the start of each relevant file.
  47. ## Escape non-namespace classes
  48. The class file you just moved is now in the `Friendica` namespace, but it probably isn't the case for all the classes referenced in this file.
  49. Since we added a `namespace Friendica\Core;` to the file, all the class names still declared in `include` will be implicitly understood as `Friendica\Core\ClassName`, which is rarely what we expect.
  50. To avoid `Class Friendica\Core\ClassName not found` errors, all the `include`-declared class names have to be prepended with a `\`, it tells the autoloader not to look for the class in the namespace but in the global space where non-namespaced classes are set.
  51. If there are only a handful of references to a single non-namespaced class, just prepending `\` is enough. However, if there are many instance, we can use `use` again.
  52. ````php
  53. namespace Friendica\Core;
  54. ...
  55. if (\DBM::is_result($r)) {
  56. ...
  57. }
  58. ````
  59. ````php
  60. namespace Friendica\Core;
  61. use Friendica\Database\DBM;
  62. if (DBM::is_result($r)) {
  63. ...
  64. }
  65. ````
  66. ## Remove any useless `require`
  67. Now that you successfully moved your class to the autoloaded `src` folder, there's no need to include this file anywhere in the app ever again.
  68. Please remove all the `require_once` mentions of the former file, as they will provoke a Fatal Error even if the class isn't used.
  69. ## Miscellaneous tips
  70. When you are done with moving the class, please run `php util/typo.php` from the Friendica base directory to check for obvious mistakes.
  71. Howevever, this tool isn't bullet-proof, and a staging install of Friendica is recommended to test your class move without impairing your production server if you host one.
  72. Most of Friendica processes are run in the background, so make sure to turn on your debug log to check for errors that wouldn't show up while simply browsing Friendica.
  73. Check the class file for any magic constant `__FILE__` or `__DIR__`, as their value changed since you moved the class in the file tree.
  74. Most of the time it's used for debugging purposes but there can be instances where it's used to create cache folders for example.
  75. ## Related
  76. * [Class autoloading](help/autoloader)
  77. * [Using Composer](help/Composer)