Skip to content

Commit 6dfa36d

Browse files
defining your namespaces done
1 parent ac5673c commit 6dfa36d

File tree

2 files changed

+115
-3
lines changed

2 files changed

+115
-3
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
sidebar_position: 3
3+
title: Defining your Namespace Prefix
4+
---
5+
# Defining your Namespace Prefix
6+
## Components
7+
When you develop a Joomla extension you define the namespace you want to use in your manifest file, eg for `com_example`:
8+
```xml
9+
<namespace path="src">Mycompany\Component\Example</namespace>
10+
```
11+
Let's look at the various parts of this:
12+
13+
- Mycompany – you are free to put whatever you like here – it's generally set to indicate the name of the company who developed the extension.
14+
15+
- Component – this must be set to Component if your Joomla extension is a component
16+
17+
- Example – this should match the name of the component you're developing
18+
19+
- src – this is the subfolder where you store your class files. You don't have to call it this, but it is common practice.
20+
21+
From this manifest file Joomla will create 2 namespace prefixes:
22+
23+
- 'Mycompany\Component\Example\Site\' will point to components/com_example/src
24+
- 'Mycompany\Component\Example\Administrator\' will point to administrator/components/com_example/src
25+
26+
(Assuming you have both site and administrator aspects to your component).
27+
## Modules
28+
You would do a similar thing for modules:
29+
```xml
30+
<namespace path="src">Mycompany\Module\Example</namespace>
31+
<files>
32+
<filename module="mod_example">mod_example.php</filename>
33+
<folder>src</folder>
34+
<folder>tmpl</folder>
35+
</files>
36+
```
37+
It's not essential to match the "Example" in the namespace to "mod_example" – the name of the module – but you'll probably find it easier to do that.
38+
39+
If this is a site module then Joomla will create the namespace prefix:
40+
41+
'Mycompany\Module\Example\Site\' will point to modules/mod_example/src
42+
43+
If this is an administration module then Joomla will create the namespace prefix:
44+
45+
'Mycompany\Module\Example\Administrator\' will point to administrator/modules/mod_example/src
46+
## Plugins
47+
For plugins:
48+
```xml
49+
<namespace path="src">Mycompany\Plugin\Content\Example</namespace>
50+
<files>
51+
<filename plugin="example">example.php</filename>
52+
<folder>src</folder>
53+
</files>
54+
```
55+
Here there's an extra part to the namespace, which must be set to the plugin type – 'Content' in the example above.
56+
57+
Once again the 'Example' segment in the namespace doesn't have to match exactly the plugin="example" name of the plugin.
58+
59+
For the above Joomla would create the namespace prefix:
60+
61+
'Mycompany\Plugin\Content\Example\' will point to plugins/content/example/src
62+
63+
## Capitalisation
64+
**Be careful to ensure that capitalisation within parts of the fully qualified classname matches capitalisation within the names of directories and files! If you develop on Windows but your target system is linux you may find that your extension works on your development machine but not on your target system. This is because Windows is forgiving if you have wrong capitalisation in your filenames or directory names – it will still open the file for you – but linux isn't.**
65+
66+
## Finding your Classes
67+
Once you have decided upon your namespaces then you can sort out where you class files are going to be located. Joomla components use a fairly flat directory structure under `src`, but you don't stick to that. As long as you adhere to the PSR4 recommendation for matching fully qualified class names to file paths then Joomla will find the source files for your classes. Gone are the days when you had to guess what directory to store your helper file in, and what to name to give the file!
68+
69+
However, there is one case where you need to give Joomla a helping hand - in your form XML files.
70+
- if you define a custom field then you need to tell Joomla where to find the class which defines that custom field
71+
- if you define a custom validation rule then you need to tell Joomla where to find the class which defines that rule
72+
73+
As XML files don't have PHP `<namespace>` statements you have to provide the equivalent, and it's easiest to do this by including an `addfieldprefix` or `addruleprefix` attribute within an XML element which encloses where you use them, eg:
74+
```xml
75+
<?xml version="1.0" encoding="utf-8"?>
76+
<form
77+
addruleprefix="Mycompany\Component\Example\Administrator\Rule"
78+
addfieldprefix="Mycompany\Component\Example\Administrator\Field"
79+
>
80+
```
81+
82+
## PHP Global namespace
83+
If you have a `<namespace>` statement in your PHP source file then PHP will by default assume that any classnames you refer to in your code will be under that namespace. So if you use any standard PHP classes such as `Exception`, you will have to precede them with a backslash `\Exception`. The backslash at the start indicates to PHP that this is a fully qualified classname, and as there is just one segment to this FQN it will be found in the global namespace.
84+
85+
Similarly if you use a function, then PHP will try to find that function under your namespace. But unlike classes, if it doesn't find the function under your namespace it will revert back to looking for it in the global namespace. So it's marginally more performant if you tell PHP not to bother looking in your namespace by prefixing the function name with a backslash, eg `\strlen(...)`. You will see this present at times in the Joomla PHP code.

docs/general-concept/namespaces/joomla-namespace-prefixes.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,41 @@ eg 'Joomla\Module\Login\Site\' points to modules/mod_login/src
2828
eg 'Joomla\Plugin\Fields\Calendar' points to plugins/fields/calendar/src
2929

3030
## Library Classes
31-
'Joomla\CMS\' points to libraries/src
31+
'Joomla\CMS\' points to libraries/src. Note that these are the classes which are described in the [API docs](https://api.joomla.org/) on the [Joomla CMS](https://api.joomla.org/cms-4/index.html) side.
3232

33-
'Joomla\SomethingElse\' points to libraries/vendor/somethingelse/src
33+
'Joomla\SomethingElse\' points to libraries/vendor/somethingelse/src.
3434

3535
eg 'Joomla\Event\' points to libraries/vendor/event/src
3636

37+
Note that these are the classes which are described in the [API docs](https://api.joomla.org/) on the [Framework](https://api.joomla.org/framework-2/index.html) side.
38+
39+
(As an aside, just be aware that if a class on the CMS side inherits from a class on the framework side, then not all of the methods available might be in the API docs. For example, the class Joomla\CMS\Application\WebApplication has methods such as `setHeader` because it extends [Joomla\Application\AbstractWebApplication](https://api.joomla.org/framework-2/classes/Joomla-Application-AbstractWebApplication.html), but this function isn't listed in [WebApplication API doc](https://api.joomla.org/cms-4/classes/Joomla-CMS-Application-WebApplication.html).)
40+
3741
If a library classname doesn't start with 'Joomla' then it's going to be found in one of the other directories under libraries/vendor/.
3842

3943
(Note that all the above is the general standard for Joomla code – you might however find the odd exception).
4044

41-
If you look in administrator/cache/autoload_psr4.php you'll see all the namespace prefixes for Joomla component, modules and plugins, together with the associated position in the file system (and also the namespace prefixes of any installed extensions).
45+
If you look in administrator/cache/autoload_psr4.php you'll see all the namespace prefixes for Joomla component, modules and plugins, together with the associated position in the file system (and also the namespace prefixes of any installed extensions).
46+
47+
## Duplicate class names
48+
Before namespacing was introduced Joomla had a lot of duplicate classnames, eg. for com_example the MVC model code would be in the class ExampleModelExample for both site and administrator, with both classes in the global namespace. This presented an obstacle to sharing code - you couldn't just have your site model class inheriting from your admin model class, for instance.
49+
50+
With Joomla namespacing, the FQNs of the site and administrator model are different, as they're in different namespaces. This makes it very easy to share code between them - you just have to let one model class inherit from the other.
51+
```php
52+
<?php
53+
namespace Mycompany\Component\Example\Site\Model;
54+
55+
use Mycompany\Component\Example\Administrator\ExampleModel as AdministratorModel;
56+
57+
class ExampleModel extends AdministratorModel {
58+
```
59+
60+
A word of warning: although Joomla has unique FQNs for all the classes, **there are some Joomla library classes which share the same last segment of the FQN**, for example:
61+
62+
- Registry may refer to Joomla\Registry\Registry (a utility class for holding data structures) or Joomla\CMS\HTML\Registry (a class for holding snippets of HTML used in `HtmlHelper::_()` calls)
63+
64+
- CategoryFactory may refer to Joomla\CMS\Extension\Service\Provider\CategoryFactory or Joomla\CMS\Categories\CategoryFactory - there are several similar instances where the same last segment of the FQN may refer to the Factory class, or to the service provider class which registers the Factory class in the dependency injection container.
65+
66+
- DispatcherInterface may refer to Joomla\CMS\Dispatcher\DispatcherInterface (the DispatcherInterface for components and modules) or Joomla\Event\DispatcherInterface (the DispatcherInterface for plugins).
67+
68+
You just need to be careful to examine the `use` statement to see exactly which class is being referred to.

0 commit comments

Comments
 (0)