⬆️ ⬇️

How to use namespaces in PHP, Part 2: importing, aliases, and name resolution rules

image

In Part 1 , we discussed what namespaces in PHP are for and what the reserved word namespace means. In this article, we explore the use statement and the way that PHP resolves namespace names.



For the purposes of this article, we will use two almost identical codes, the only difference of which is in their namespaces:



lib1.php:


<?php // application library 1 namespace App\Lib1; const MYCONST = 'App\Lib1\MYCONST'; function MyFunction() { return __FUNCTION__; } class MyClass { static function WhoAmI() { return __METHOD__; } } ?> 


lib2.php:


 <?php // application library 2 namespace App\Lib2; const MYCONST = 'App\Lib2\MYCONST'; function MyFunction() { return __FUNCTION__; } class MyClass { static function WhoAmI() { return __METHOD__; } } ?> 


Before we begin, let's recall a few definitions from PHP terminology:



Fully-qualified name (Fully-qualified name)


Any PHP code can refer to the full qualified name — an identifier starting with a namespace delimiter (that is, a backslash, backslash), for example: \ App \ Lib1 \ MYCONST, \ App \ Lib2 \ MyFunction (), etc.

')

There is no ambiguity in full qualified names. The initial backslash acts in the same way as the file path, denoting the “root” of the global space. If we were performing various MyFunction () in our global space, they could be called from lib1.php or lib2.php using \ MyFunction ().



Full qualified names are useful for one-time function calling or object initialization. However, when you make many calls, they become impractical. As we will learn below, PHP offers other options in these cases.



Qualified name


An identifier that has at least one namespace separator (the namespace separator is, in fact, a backslash), for example, Lib1 \ MyFunction ().



Unqualified name


An identifier without a namespace delimiter, such as MyFunction ().



Work with the same namespace



Let's discuss the following code:



myapp1.php:


 <?php namespace App\Lib1; require_once('lib1.php'); require_once('lib2.php'); header('Content-type: text/plain'); echo MYCONST . "\n"; echo MyFunction() . "\n"; echo MyClass::WhoAmI() . "\n"; ?> 


Although we have added both lib1.php and lib2.php, the MYCONST, MyFunction and MyClass identifiers will apply to lib1.php only. This will happen because the code myapp1.php is located in the namespace that is the same with App \ Lib1:



result:


 App\Lib1\MYCONST App\Lib1\MyFunction App\Lib1\MyClass::WhoAmI 


Importing Namespaces (Namespace Importing)



Namespaces can be imported using the use statement, for example:



myapp2.php:


 <?php use App\Lib2; require_once('lib1.php'); require_once('lib2.php'); header('Content-type: text/plain'); echo Lib2\MYCONST . "\n"; echo Lib2\MyFunction() . "\n"; echo Lib2\MyClass::WhoAmI() . "\n"; ?> 


You can import one or more namespaces with use, separating them with a comma. In this example, we imported the App \ Lib2 namespace. We still cannot link directly to MYCONST, MyFunction or MyClass because our code is in the global space and PHP will look for them there. But if we add the prefix “Lib2 \”, they will become qualified names, and PHP will search for them in the imported namespaces until it finds a complete match.



result:


 App\Lib2\MYCONST App\Lib2\MyFunction App\Lib2\MyClass::WhoAmI 


Namespace aliases (Namespace Aliases)



Namespace aliases are probably the most useful construct. Aliases allow you to refer to long namespaces using a short name.



myapp3.php:


 <?php use App\Lib1 as L; use App\Lib2\MyClass as Obj; header('Content-type: text/plain'); require_once('lib1.php'); require_once('lib2.php'); echo L\MYCONST . "\n"; echo L\MyFunction() . "\n"; echo L\MyClass::WhoAmI() . "\n"; echo Obj::WhoAmI() . "\n"; ?> 


The first use statement defines App \ Lib1 as "L". Any qualified name that uses “L” will be converted to “App \ Lib1” at compile time. Therefore, we would rather refer to L \ MYCONST or L \ MyFunction than to the full qualified name.



The second use statement is more interesting. He defines “Obj” as an alias for the class “MyClass” within the App \ Lib2 \ namespace. This operation is applicable only for classes - not for constants or functions. Now we can use new Obj (), or call static methods, as shown above.



result:


 App\Lib1\MYCONST App\Lib1\MyFunction App\Lib1\MyClass::WhoAmI App\Lib2\MyClass::WhoAmI 


Naming rules



PHP identifier names are resolved by the following namespace rules. For more information, refer to the PHP manual ( English / Russian ).

  1. Invoking qualified functions is allowed at compile time.
  2. All qualified names are translated at compile time according to the current imported namespaces. For example, if the namespace A :: B :: C is imported, the call to C :: D :: e () will be translated as A :: B :: C :: D :: e ().
  3. Inside the namespace, all qualified names are translated according to the import rules, for example, if the namespace A \ B \ C is imported as C, the call C \ D \ e () is translated into A \ B \ C \ D \ e ().
  4. Unqualified class names are translated at compile time according to the current imported namespaces and full names replace the short imported names, for example, if class C in the namespace A \ B is imported as X, new X () will be translated into new A \ B \ C ()
  5. Within the namespace, the call to unqualified functions is interpreted at compile time. For example, if MyFunction () is called within the A \ B namespace, PHP first looks for the \ A \ B \ MyFunction () function. If it is not found, PHP will search for \ MyFunction () in the global space.
  6. Calls to unqualified or qualified class names are interpreted at compile time. For example, if we call new C () within the A \ B namespace, PHP will look for the class A \ B \ C. If it is not found, it will lead to an attempt to autoload A \ B \ C.


In the next part: keywords and autoload.



See also:



How to use namespaces in PHP, Part 1: Basics

How to use namespaces in PHP, Part 3: Keywords and autoloading



Notes:





A / Remarks, amendments, indications of inaccuracies and so on. - welcome !



B / Code highlighted using Source Code Highlighter .

Source: https://habr.com/ru/post/72097/



All Articles