What is Namespaces?

Like many languages, PHP does not allow you to have two classes with the same name at a global level. While on the surface this may sound like a good idea, it becomes a problem when you import libraries and a name collision occurs (completing libraries have one or more classes with the same name). To solve this issue the concept of "name spaces" was added in PHP 5.3.

A name space allows you to partition code into logical groups by defining them into their own “namespace”. A text string after the “namespace” keyword identifies the name space and all code below it then lives within that name space. Name spaces also provide a way to group interfaces functions and constants.

Namespaces can be defined as a class of elements in which each element has a unique name to that associated class. It may be shared with elements in other classes.

Declaration of namespace

The use keyword is used for creation of namespace.

File name : index.php

use <namespace-name>;

The default namespace used in Laravel is App, however a user can change the namespace to match with web application. Creating user defined namespace with artisan command is mentioned as follows −

File name : index.php

php artisan app:name SocialNet

The "namespace" keyword must have no other keywords before it but comments are OK and needed for most documentation generators.

What if you don't use namespaces?

File name : index.php

In a Laravel application, you can't take advantage of custom directories if you don’t use a name space., Instead, you need to add additional paths to your composer.json file to include the paths.





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here


Ittutorial