Laravel Namespaces

In this topic, we will learn about namespace in Laravel.

namespaces and use are not Laravel features. These are part of Core PHP's functionality and can be used just like any other constructs.

Namespacing is designed in PHP to solve two problems:

1. Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.

2. Ability to alias (or shorten) Extra_Long_Names designed to alleviate the first problem, improving readability of source code.

In PHP a namespace is a container that encapsulates some "stuff": functions classes, interfaces or traits. It's done to avoid naming conflicts, because in PHP (or most languages) you can only declare a class/function/interface/trait one time and one time only

namespaces basically group your functions, classes and constants under a particular 'name', which we call a namespace.

Note: A namespace declaration must be the first thing in the PHP file. The following code would be invalid: