Skip to content

Commit 9369492

Browse files
First page on namespacing done
1 parent 309e67c commit 9369492

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"githubPullRequests.ignoredPullRequestBranches": [
3+
"main"
4+
]
5+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
sidebar_position: 1
3+
title: Finding Class files with PSR4
4+
---
5+
Namespaces allow us to organise our PHP classes in a similar way that directories allow us to organise our files. In particular, the [PSR4 recommendation](https://www.php-fig.org/psr/psr-4/) which Joomla uses enables us to determine what file a certain class should be in.
6+
7+
To see this, let's take an example of the file administrator/components/com_content/src/View/Articles/HtmlView.php, which contains the code for the view which presents the Articles to the administrator. This file contains the lines
8+
```php
9+
namespace Joomla\Component\Content\Administrator\View\Articles;
10+
...
11+
class HtmlView extends BaseHtmlView { …
12+
```
13+
so the fully qualified name (FQN) of the class is `\Joomla\Component\Content\Administrator\View\Articles\HtmlView`.
14+
15+
Joomla holds a list of namespace prefixes strings, and it compares the string containing the fully qualified class name to that list, starting at the left hand side of the class name, to see if it can find a partial match. In this example the namespace prefix `'Joomla\Component\Content\Administrator'` will match.
16+
17+
This namespace prefix has an associated directory within the file system, which is the starting point for finding classes. In this case the directory is administrator/components/com_content/src. (We'll see shortly how the namespace prefixes and associated directories are defined).
18+
19+
Next it removes the matching namespace prefix from the FQN, leaving \View\Articles\HtmlView, and treats this as the path down from the above directory. So the full file path is: administrator/components/com_content/src/View/Articles/HtmlView.php. The diagram gives a pictorial view of this.
35 Bytes
Loading
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Namespacing was progressively introduced for Joomla classes over a number of Joomla 3.x releases. In Joomla 4 nearly all of the Joomla classes were namespaced, and it was expected that Joomla extensions were namespaced as well.
2+
3+
If you're not familiar with PHP namespacing then you can find general information online, for example at https://www.php.net/manual/en/language.namespaces.php
4+
5+
This Joomla documentation focuses on how namespacing is implemented within Joomla.

0 commit comments

Comments
 (0)