Monday 7 May 2018

Namespaces overview- PHP Training

What are namespaces in PHP?


In the extensive definition namespaces are a way of enclose items. This can be seen as an 
abstract access in many places. For example, in any operating system agenda serve to group 
similar files, and act as a namespace for the case within them. As a concrete example, the file  
foo.txt can continue in both directory /home/greg and in /home/other, but two copies of foo.txt 
cannot co-exist in the duplicate directory. In extensions, to access the foo.txt file foreign of the
 /home/greg directory, we must assume the directory name to the filename adoption the directory 
separator to get /home/greg/foo.txt. This same principle develop to namespaces in the programming 
world. Learn More about namespace form PHP Training in Chandigarh.

Namespace in PHP

In the PHP world, namespaces are arrange  to solve two problems that authors of libraries and 
applications encounter when creating recyclable code aspect such as classes or functions:
  1. Name culmination between code you fabricate, and household PHP 
    classes/capacities/constants or outsider classes/capacities/constants.
  2. Capacity to other wise(or abbreviate) Extra_Long_Names intended to alleviate the
     primary issue, enhancing believability of source code.

Why Do We Need Namespaces in PHP?

As the size of your PHP code study increases, the more likely you will unwitting discuss a
 function or class name that has been announced before. The problem is distributed if you 
attempt to add third-party ingredient or plugins; what if two or more code sets device a 
‘Database’ class. The only solution has been long class/function names. For example,
 WordPress prefixes every name with ‘WP_’. The Zend Framework uses a deeply 
descriptive naming meeting that results in long-puffing class names .

How are Namespaces Defined?

By default, all consistent, class and function names are arranged in a global space — like 
they were before namespaces were backed.
Namespaced code is decide using a single namespace keyword at the top of your PHP file.

Sub-namespaces
PHP allows you to define a ranking of namespace names so libraries can be subdivided. 
Sub-namespaces are detached using a backslash (\) character, e.g.
  • MyProject\SubName
  • MyProject\Database\MySQL
  • CompanyName\MyProject\Common\Widget
PHP Training in Chandigarh is the best place to learn PHP for developing websites using PHP.

0 comments:

Post a Comment