PHP Basic Part-4: What is Namespace in PHP?

PHP Basic Part-4: What is Namespace in PHP?
Image

What is namespace? I will tell you a story to understand this. Amidur and Robert Azar are two brothers, one of them is 4 years old and the other is 3 years old. You have to give them the same kind of toy to see. And if they don’t look exactly the same, they argue all the time about which is more beautiful or which is less beautiful. And so their father always buys two identical toys. Meanwhile, being the same, their father got into another problem, arguing and arguing over each other’s toys, because no one could understand who was using the toys.

To solve this problem, their parents put a level on each of their toys. So that everyone can recognize his own. Now there is no problem. Namespace is a type of level or sticker. Again, the concept of Namespace in PHP is a lot like putting two files of the same name in two different folders. That is, like foo / info.txt and bar / info.txt path. However, in order to actually declare a namespace, we need to access it with a virtual path instead of creating such a folder.

Basically, when you code in PHP, we often have problems with the name of the class, interface, trait, function or constant. It turns out that another library with the same name, class, interface, trait, function or constant in the name of the class, function or constant that I used in a code, or another member of your team with the same name class, interface, trait, function or constant Has created, which you may need to use later. Result – Conflict with name. The namespace feature has been added to PHP 5.3 to solve this problem.

With Namespace, each Code Block can be given a different name, and then the code of that code block can be used with that name. If any other code in the script has a class, interface, trait, function or constant of the same name, PHP will not show any error. When you call a class, interface, trait, function or constant by namespace, PHP will understand exactly which code to execute now.

How to define php namespace?

To define a namespace in PHP, first the namespace keyword and then the namespace. Namespace declaration should start from the very top of the PHP file, ie before any other code. The only option is to declare the keyword before the namespace decleration. You can declare more than one namespace in the same file. Again, you can declare different namespaces in different files. Below is the syntax of the namespace declaration:

Namespace without subNamespace

Image

Namespace With Sub Namespace

Image

How to call constant, function and class with namespace?

To better understand the call of constant, function and class with namespace, first we will create two different files with constant, function and class of the same name with two different namespaces.

File: myproject1.php

Image

File: myproject2.php

Image

Now we will include myproject1.php and myproject2.php files in the project.php file. And call with namespace.

File: project.php

Image

Output

Image

How to define multiple namespaces in the same file?

You can define multiple namespaces in the same file in two ways. The first rule is the same as declaring a common namespace, i.e. declaring the namespace after the namespace keyword. And all the code written in it after the first namespace announcement is included in the first namespace until the next namespace announcement. See the following example:

Image

You can also define multiple namespaces in a single file using curly brackets. See the following example:

Image

Note that you cannot use bracket namespace and unbracket namespace together if you wish. PHP will show a fatal error. The following example will show you a fatal error.

Image

How to call namespaces of the same file with multiple namespaces?

If you want to call the namespaces of the same file with multiple Php namespaces, you need to use the use keyword. See the example below:

Image

Namespace Importing

You can import namespace with use keyword in PHP. And you do not have to enter the full namespace path. Just enter the name of the latest namespace. We will create two files to understand namespace import.

file1.php

Image

Now we will import “namespace User \ Member \ Profile” in file2.php with use keyword. And we don’t have to call constant, function and class using the whole path. All you have to do is use the last path profile. See the following example:

file2.php

Image

Output

Image

However, in case of class, if you add the class name as the last path when importing. Then you no longer have to use the namespace name when objecting from that class. Write the file file2.php as follows:

Image

Notice, last time we had to create the object from the class with the last namespace, and this time we didn’t have to create the object from the class with that last namespace.

Namespace Aliasing

Aliasing is a lot like a shortcut link for windows or a symbolic link for Linux. Aliasing is used to shorten many long namespace paths. The keyword as is used to aliasing the namespace. See the following example:

file2.php

Image

Namespace name definitions

There are three types of namespace names based on the use of namespace:

  1. Unqualified Namespace
  2. Qualified Namespace
  3. Fully Qualified Namespace

Unqualified Namespace

When no namespace is used when calling a constant, function or method, it is called Un-Qualified Namespace. Below is an example of Unqualified Namespace:

Image

Qualified Namespace

When namespace paths are used at least when calling a constant, function or method, it is called Qualified Namespace. Below is an example of Qualified Namespace:

Image

Fully Qualified Namespace

When a constant, function or method call uses the full path or alias of the Full Path of the namespace, it is called Fully Qualified Namespace. Below is an example of Fully Qualified Namespace:

Image

Leave a Reply

Your email address will not be published. Required fields are marked *