Views: 15258
Last Modified: 27.05.2021

Name spaces

The notion of name spaces permits giving system elements clearer names, getting rid of numerous name prefixes, and also avoiding potential conflicts. All classes delivered in a standard installation package must be located in the Bitrix name space that does not overlap either with PHP or with partner implementations. Each standard module determines its subspace in the Bitrix name space which coincides with the module name. For example, for the forum module Bitrix\Forum will be the name space, and for the main module – Bitrix\Main.

Note. For partner classes, the namespace can be as follows:
namespace Asd\Metrika;  
 
class CountersTable extends Entity\DataManager  
{  
   ....

This means that this class (in /lib/) belongs to the module asd.metrika and it can be addressed as follows (after the indicated module is connected):

\Asd\Metrika\CountersTable::update();

The class itself is located in the file asd.metrika/lib/counters.php.

If necessary, a module may organize subspaces inside its name space. For example, Bitrix\Main\IO, Bitrix\Forum\Somename\Somename2. But this option should be used only if it is justified for the organization of a correct architecture of this module.


Naming Rules

  • Name spaces must be named in “UpperCamelCase.”
  • Names cannot contain any symbols but for Latin letters.
  • Class name must be a noun. Unnecessary acronyms and abbreviations should be avoided.

Examples:

namespace Bitrix\Main\Localization;
namespace Bitrix\Main\Entity\Validator;

Abbreviations that are not generally accepted (in Bitrix Framework) cannot be used.

Attention! The examples that are provided does do not mention name spaces. It make the text more readable and easier to comprehend. Prior using the examples provided in the documentation, a name space must be added.

It ca be done as follows:

  • By using PHPdoc;
  • By using IDE;
  • additionally, the documentation context usually clarifies which class is described.

Full address line can be abridged. Instead \Bitrix\Main\Class::Function() you can specify Main\Class::Function().


Synonyms also can be used instead of long name spaces. To do it, use is inserted. For example, the following long structure is available:

\Bitrix\Main\Localization\Loc::getMessage("NAME");

To abridge it, declare a synonym at the start of the file and use an abridged variant of the call afterwards:

use \Bitrix\Main\Localization\Loc;
...
Loc::getMessage("NAME");

Related links:




Courses developed by Bitrix24