📜 ⬆️ ⬇️

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

image
In Part 1 and 2 of our series of articles, we looked at the basics of the namespace , as well as importing, aliases, and the rules for parsing names . In this final article we will discuss some more advanced namespace options.

__NAMESPACE__ constant


__NAMESPACE__ is a PHP string that always returns the current namespace name. In global space, it will always be an empty string.
<?php namespace App\Lib1; echo __NAMESPACE__; // outputs: App\Lib1 ?> 

Its value has an obvious benefit during debugging. It can also be used to dynamically generate the full qualified class names, for example:
 <?php namespace App\Lib1; class MyClass { public function WhoAmI() { return __METHOD__; } } $c = __NAMESPACE__ . '\\MyClass'; $m = new $c; echo $m->WhoAmI(); // outputs: App\Lib1\MyClass::WhoAmI ?> 

Namespace keyword


The namespace keyword can be used to explicitly indicate the source of a position (give it a link) within the current namespace or namespace. Equivalent namespace is the self keyword for classes:
 <?php namespace App\Lib1; class MyClass { public function WhoAmI() { return __METHOD__; } } $m = new namespace\MyClass; echo $m->WhoAmI(); // outputs: App\Lib1\MyClass::WhoAmI ?> 

Autoload Namespace Classes


One of the best time saving features in PHP 5 is autoload. In the global (not related to any namespace) PHP code, the standard autoload function can be written as follows:
 <?php $obj1 = new MyClass1(); // classes/MyClass1.php is auto-loaded $obj2 = new MyClass2(); // classes/MyClass2.php is auto-loaded // autoload function function __autoload($class_name) { require_once("classes/$class_name.php"); } ?> 

In PHP 5.3, you can create an instance of a namespace class. In this situation, the full qualified namespace names and class names are passed to the __autoload function, for example, the value of $ class_name may be " App \ Lib1 \ MyClass ". You can continue to place your PHP class files in the same folder and select the namespaces from the string, however, this can lead to a conflict of file names.

Alternatively, the file hierarchy of your classes can be organized in the same way as the structure of your namespaces. For example, the file MyClass.php can be created in the / classes / App / Lib1 folder :

/classes/App/Lib1/MyClass.php:

 <?php namespace App\Lib1; class MyClass { public function WhoAmI() { return __METHOD__; } } ?> 

A file in the root directory can use it with the following code:
')
myapp.php:

 <?php use App\Lib1\MyClass as MC; $obj = new MC(); echo $obj->WhoAmI(); // autoload function function __autoload($class) { // convert namespace to full file path $class = 'classes\' . str_replace('\\', '/', $class) . '.php'; require_once($class); } ?> 

Explanation:

  1. Use MC alias for App \ Lib1 \ MyClass
  2. During compilation new MC () is translated to new App \ Lib1 \ MyClass () .
  3. The string " App \ Lib1 \ MyClass " is passed to the __autoload function. This replaces all namespace slashes with forward path slashes of the file, and modifies the string so that the " classes \ App \ Lib1 \ MyClass.php " file is loaded.


I hope that this series of articles on PHP namespaces is useful to you. Will you use namespaces in your PHP code?

See also:


How to use namespaces in PHP, Part 1: Basics
How to use namespaces in PHP, Part 2: Importing, aliases, and name resolution rules

Notes:


A / While working on the translation, when I was searching for another term, I found here the same third part, but already translated. In my opinion, that translation contains rather gross errors, compare:
The namespace keyword can be used to make a clear connection between the current namespace and subspaces like self in classes.

My translation:
The namespace keyword can be used to explicitly indicate the source of a position (give it a link) within the current namespace or namespace. Equivalent namespace - the self keyword for classes


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

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


All Articles