·  PHP – Syntax – Keywords – use


- Info:

  - use keyword was introduced in PHP 5 together with namespaces.

  - It is used to create short alias for full class or interface names (these are prefixed with namespace name):

    use ivoronline\test\MyClass; #MyClass is alias for full class name \ivoronline\test\MyClass.

  - Last word (after last '\') simply becomes alias/replacement for complete name.

    So when you use this word in a code it will be simply replaced with complete name and '\' will be added at the begining.

  - This means that alias could also be used for namespace or part of it but this generally leads to confusion.

  - use keyword can be used with as keyword but this usually leads to even more confusion:

    use ivoronline\test\MyClass as MyClass12345; #Alias is now MyClass12345 instead of MyClass.


- Class Alias: 

  - This example shows how to use use keyword to to make alias for fully qualified class name.

  - Create PHP Console Application C:\Temp\PHP\Test.php





      namespace ivoronline\test;


      class MyClass {                            #Full class name is \ivoronline\test\MyClass

        static public function displayString() {

          echo("Hello from MyClass!\n");









      include("MyClassFile.php");                #Copy file content with full class name.


      use ivoronline\test\MyClass;               #MyClass is alias for \ivoronline\test\MyClass


      \ivoronline\test\MyClass::displayString(); #Full class name.

                       MyClass::displayString(); #MyClass is replaced with \ivoronline\test\MyClass