Namespaces in PHP is similar to namespaces in C++ that encapsulates items by reusing names in order to avoid name conflicts. It is also viewed as an abstract concept in many situations. It permits in re-declaration of the same classes or interface or functions or constant functions in a distinct namespace without receiving the fatal error. This namespace is a code block that is labeled hierarchically that holds a normal PHP code. It consists of a PHP code which is valid. Moreover, it affects code types such as classes, functions, constants, and interfaces. A namespace is declared using the keyword namespace.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
namespace sample;
Here, a namespace sample is declared.
Similar to other PHP identifiers, the namespace should also follow some rules. That is, a namespace should begin with an underscore or letter, followed by letters, underscores, or numbers.
Let us imagine a namespace as a cupboard where we can keep all items such as dresses, accessories, etc. For each person, there will be a separate cupboard which can store their items. In order to identify each person’s cupboard, a label can be given to each of them so that any confusion won’t occur. In the past, programmers separated code bases using underscores in the constants, classes, and functions. It is similar to labeling everyone’s items and keeping them in a big cupboard. Even though it is organized, efficiency won’t be higher.
In this situation, the namespace concept appears. The same class, interface, function, and constant can be declared in different namespaces without any fatal errors. Basically, the namespace is a code block which is hierarchically labeled that holds a normal PHP code.
As we have already mentioned, namespaces in PHP encapsulate items by reusing names to avoid name conflicts.
For example, consider a PHP program as mentioned below.
Code:
<?php namespace sample ; function func() { echo ' Never give up! Keep trying . . . ' ; } // Resolves to the function func() ; ?>
It can be seen that a function func is present, and it prints a text on executing the code.
As the PHP code library size increases, there is a chance of unintentionally reusing a class name or function which is already declared. The issue is exacerbated if third-party components and plugins are added. Moreover, name collision issues can be solved with the help of namespaces. PHP constants, functions, and classes can be gathered together into namespace libraries. These namespaces can follow a specific hierarchy, similar to the folders available in the file system on the computer. The concept, Sub-namespaces, is very useful for arranging the project structure. If the project needs database access, all the codes related to the database, like exceptions of a database, connection handlers, can be kept in a sub-namespace called DB.
For maintaining the flexibility, it is better to save sub-namespaces in sub directories. This inspires project structuring as well as makes it easy to use auto-loaders that follow the standard PSR-0.
Normally, a backslash will be used in PHP as a namespace separator.
SampleSub Sample
SampleSubSampleSamp
CollegeProjCommonWidget
In the cases where a declaration of namespace is done globally, it can be declared without using any name, as shown below.
Code:
<?php namespace { // Global . . . ! } ?>
In some cases, multiple namespaces can be used within one PHP code, as shown below.
Code:
<?php namespace sam { } namespace samp { } namespace { } ?>
Code:
<?php echo "Never Give up ! " ; namespace Html ; . . . ?>
Given below are the examples of PHP namespace:
PHP program that uses a namespace and calls a function both implicitly and explicitly.
Code:
<?php namespace sample; function func() { echo 'Never give up! Keep trying. . .'; } // Resolves to the function func(); // Explicitly resolves to the function namespace\func(); ?>
Output:
In the program, a namespace sample is created first. After that, it can be seen that a function func is defined, and it prints a text on executing the code. As the function is called implicitly and explicitly, the text gets printed twice.
Suppose the namespace is declared after defining the function, as shown below. Then, what will happen?
Code:
<?php function func() { echo 'Never give up! Keep trying. . .'; } // Resolves to the function func(); namespace sample; // Explicitly resolves to the function namespace\func(); ?>
Yes…. An error will be displayed, as shown below. That is, namespace should be defined at the beginning of the program.
Output:
PHP program that uses a namespace and calls a function for printing table details.
Code:
<?php namespace Html; class sampletable { public $heading= ""; public $cntrows= 0; public function fnc() { echo "<p> The Table used is '{$this->heading}' that has {$this->cntrows} different rows. . . .</p>"; } } $tb= new sampletable (); $tb->heading= " Students "; $tb->cntrows= 8 ; ?> <!DOCTYPE html> <html> <body> <?php $tb->fnc(); ?> </body> </html>
Output:
In the program, a namespace Html is created first. After that, a class sampletable is created with variables heading, cnt rows. A function func is also present, which contains a text. Heading students and 8 as count of rows were given after that. On executing the code, the line gets printed with the table name and number of rows.
Namespaces in PHP is similar to namespaces in C++ that encapsulates items by reusing names in order to avoid name conflicts. In this article, different aspects such as syntax, working, and examples of size() functions are explained in detail.
The above is the detailed content of PHP namespace. For more information, please follow other related articles on the PHP Chinese website!