1

PHP starts with the index.php, then index.php includes app.php, and then includes framework.php. This is where my confusion starts, maybe because of my lack of knowledge about namespaces.

In framework.php, I found:

use Joomla\CMS\Version;
use Joomla\Utilities\IpHelper; 

I don't think this Joomla\CMS\Version namespace is just casually written, it must be referring to some code within a file, but which file and which code is still a mystery to me.

The codes below appears without a prior declaration code:

new Version()
IpHelper::setAllowIpOverrides(true);

My guess, it is related to the namespace above, but I don't know the actual relation, if my guesses are correct, how can use Joomla\CMS\Version logically give birth to a new Version()?

2 Answers 2

0

The class named Version, the code you are looking for, is found in ..\libraries\src\Version.php

The use Joomla\CMS\Version statement is the declaration or 'require_once' for the Version class and that makes it available for the instantiation with the new Version() statement.

Any namespace prefixed with Joomla\CMS\ can be found in ..\libraries\src

3
  • If you are trying to find your way around the Joomla system, or PHP in general, I would recommend using an advanced editor like PHPStorm. One of its features is that it enables you to click on a class name to open up the PHP file for that class and see where it's located. If you combine PHPStorm with a debugger like Xdebug, you can also do things like step through the execution of the code line by line and see how it all works. It can take a bit of setting up depending on your environment, but it's worth the effort if you are planning to do much development work.
    – Grant Hood
    Apr 5, 2023 at 11:42
  • I don't understand, [use Joomla\CMS\Version] then linked to [..\libraries\src\Version.php], even the PATH doesn't seem similar to me, or are there some codes that facilitate the linking process ?, or should I read a document to understand this? Apr 13, 2023 at 15:05
  • @dhanysaguntoro if you look at ..\administrator\cache\autoload_psr4.php in your Joomla installation you will see this file creates a map between the namespace and where the file actually resides. Each time certain extensions(component,library) are installed/updated then this file is deleted and regenerated based on the value in the <namespace> element in your manifest.xml. If you delete autoload_psr4.php then it will be regenerated the next time your website is invoked. Reading is good, try this dionysopoulos.me/book/concepts.html#concepts-namespaces
    – Irata
    Apr 13, 2023 at 22:43
0

Joomla uses Composer package manager to manage its dependencies and to autoload its classes. Composer autoloader follows PSR-4 autoloading standard. In case you've never heard of these before, start by reading the linked documentation.

In short, Composer projects define their dependencies, autoloading rules and other project settings in a composer.json file. But Joomla CMS does not ship this file as the CMS doesn't (yet) support managing dependencies after installation (e.g. extensions can't use the top level Composer instance). You can see this file when running the CMS from a development environment, (i.e. from the repository). In this file you'll find this autoload directive:

"autoload": {
    "psr-4": {
        "Joomla\\CMS\\": "libraries/src/"
    }
}

What it means is that classes in libraries/src/ follow PSR-4 autoloading and have Joomla\CMS\ namespace prefix. PSR-4 standard essentially maps class name to a file name, using namespace separator as directory separator and adding the php extension. Therefore, Joomla\CMS\Version class is mapped to libraries/src/Version.php file. And a deeper namespace class like Joomla\CMS\Application\SiteApplication would be mapped to libraries/src/Application/SiteApplication.php file.

Joomla\Utilities\IpHelper, meanwhile, is not a class of the CMS but a class of a dependency. Particularly, joomla/utilities package. It is required in the composer.json file of the CMS:

"require": {
    "php": "^7.2.5",
    "joomla/application": "^2.0.4",
    "joomla/archive": "^2.0.2",
    "joomla/authentication": "^2.0.1",
    ...
    "joomla/utilities": "^2.0.1",
    ...
}

This package also has its own composer.json file which contains the autoload directive:

"autoload": {
    "psr-4": {
        "Joomla\\Utilities\\": "src/"
    }
}

Normally Composer dependencies are installed in vendor directory but Joomla uses a custom directory defined using vendor-dir configuration option. Back in the composer.json of the CMS we can see this:

"config": {
     ...
    "vendor-dir": "libraries/vendor",
     ...
}

To build the path to classes of dependency packages, use the vendor directory (in this case libraries/vendor), package name (joomla/utilities) and the directory defined in the autoload configuration (src/). So ultimately, the root directory of Joomla\Utilities namespace is libraries/vendor/joomla/utilities/src/. And Joomla\Utilities\IpHelper class is in libraries/vendor/joomla/utilities/src/IpHelper.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.