What are PHP Namespaces?
What are Namespaces in general?
A Layman answer with an example would be great.
What are PHP Namespaces?
What are Namespaces in general?
A Layman answer with an example would be great.
Since it’s easier to learn about the keyword “use” by knowing about “namespace”, let me explain namespace first by looking at a basic Laravel project.
There is a controller class with the name: Controller.php which is in the path: app/Http/Controllers from the project’s root directory There is also another controller class named: Controller.php, but this one is in the path: vendor/laravel/framework/src/Illuminate/Routing from the project’s root directory
You don’t need to look at the source code yet if you’re new to php because it can confuse you, instead let me explain to you the part of it that we care about and will help us understand “namespace” and “use”.
So as a matter of fact, our first controller class: app/Http/Controllers/Controller.php needs to use the Second controller class vendor/laravel/framework/src/Illuminate/Routing/Controller.php. it actually needs to extend this Class in order to have access to its content for handling some crucial routing functionalities.
So how can a class extend another class that has the same name?
class Controller extends Controller? this will NOT work,
unless there is a way to distinguish these two classes and that’s where namespace comes handy and the use keyword
helps to complete the mission of, allowing the usage of; (classes; methods; interfaces and constants), with the same name, in the same scope.
now how is it done in the code? very simple! if we look at app/Http/Controllers/Controller.php source code, we can see at the top of the class
namespace is declared as: namespace App\Http\Controllers, so this is how you give your class a namespace so it can be referenced by other classes
now this looks the same as the path to this class from the project’s root directory, with little difference and that’s the usage of “\” instead of “/“
(same as command prompt in windows),
but there is another difference and that’s the App with capital ‘A’ in the namespace versus ‘app’ with
Lowercase ‘a’ in the path. Also note that namespace is case-sensitive.
So namespace is a separate concept than the path, it can follow the path structure if it helps but it doesn’t have to be exact path to the class, method, interfaces or constants for example take a look at: vendor/laravel/framework/src/Illuminate/Routing/Controller.php source code,
we see at the top of the class the namespace is declared as: Illuminate\Routing
now let’s take look at the “use” keyword,
we use, the “use” keyword to make our class aware of a specific class or function that we want to use in our class
so we are not importing or including anything we’re just letting our class know that we will be using a specific class or
method by referencing them by their namespace
let’s take a look at app/Http/Controllers/Controller.php source code,
as you can see from the line: “use Illuminate\Routing\Controller as BaseController”, the “use” keyword followed by namespace
for the target class
(note that Illuminate\Routing\Controller.php and Illuminate\Routing\Controller ‘without .php extension’ are interchangeable)
we can use the “as” keyword along with the “use” keyword to give a specific class, method, interfaces or constants an alias which allows
app/Http/Controllers/Controller.php to extend Illuminate\Routing\Controller.php as BaseController in the line:
“class Controller extends BaseController”.
Namespace is used to enclosed group of codes so that they can be used in different places without name conflicts. Think about this as jQuery no conflict method and you will understand it better.
A namespace is a simple system to control the names in a program.
It ensures that names are unique and won’t lead to any conflict.
You can use namespace to avoid name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants. Namespaces also have the ability to alias (or shorten) Extra_Long_Names designed to reduce the first problem, improving readability of source code.
As we all know, namespaces and traits are not new in PHP, but still many php developers don’t use these Great concepts because of their complexity. So, in this post. I’ll try to clear them with examples. What are namespace and traits?
How you can implement them in your code to make your code reusable and extensible?
Benefits of namespaces
You can use namespace to avoid name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.
Namespaces also have the ability to alias (or shorten) Extra_Long_Names designed to reduce the first problem, improving readability of source code. Let’s understand namespaces with an example. create a folder name “php_oops” in htdocs(xampp) or www (xwamp) create a new folder under root directory named “namespaces”, & then create a file index.php under namespaces folder.
<?php
// FilePath:- namespaces/index.php
// let's say, we have two classes in index,
// here, these two classes have global space
class A
{
function __construct()
{
echo "I am at Global space, Class A";
}
}
class B
{
function __construct()
{
echo "I am at Global space, Class B";
}
}
// now create an object of class and
$object = new A; // unqualified class name
echo "<br/>";
$object = new \B; // fully qualified class name
// output:
I am at Global space, Class A
I am at Global space, Class B
Reference- https://medium.com/@akgarg007/php-laravel-namespaces-and-traits-01-9540fe2969cb
We often need to give resource a name, a label that will help us understand and talk about what it is. But naming is not just the simple task of assigning a sequence of characters. Names serve to distinguish one thing from another.
Even though an identifier refers to a single resource, this does not mean that no two identifiers are identical.
We can prevent or reduce identifier collisions by GUID or adding information about the namespace. namespace is the the domain from which the names or identifiers are selected. When we add namespace to an identifier, we create a qualified names.
Example Time!
suppose there is only one JOHN_SMITH in zip code 99501. There is also only one JOHN_SMITH in zip code 86302. So, when we mention JOHN_SMITH, how do we know which JOHN_SMITH we are talking about?
When we are talking in the context of zip code 99501, and mention JOHN_SMITH, we are talking about the JOHN_SMITH who leaves in zip code 99501.
<?php
namespace zc99501;
const JOHN_SMITH = "John Smith from 99501";
?>
When we are talking in the context of zip code 86302, and mention JOHN_SMITH, we are talking about the JOHN_SMITH who leaves in zip code 86302.
<?php
namespace zc86302;
const JOHN_SMITH = "John Smith from 86302";
?>
Now what happens if one person from zip code 99501 and another person from zip code 86302 want to talk about JOHN_SMITH They have to say JOHN_SMITH from 99501 did this and JOHN_SMITH from 86302 did that.
<?php
namespace zc99501;
const JOHN_SMITH = "John Smith from 99501";
namespace zc86302;
const JOHN_SMITH = "John Smith from 86302";
namespace Test;
echo \zc99501\JOHN_SMITH . PHP_EOL;
echo \zc86302\JOHN_SMITH . PHP_EOL;
?>
Here, \zc99501\JOHN_SMITH and \zc86302\JOHN_SMITH are qualified names.
Another Example.
Suppose in one context we mean Book Title when we use the constant title. And Author Name By name.
<?php
namespace Book;
const title = "Don Quixote";
const name = "Miguel de Cervantes Saavedra";
?>
in another context we mean Title of a Character by title. And the Character's Name By name.
<?php
namespace Character;
const title = "Sir";
const name = "Sancho Panza";
?>
When we want title and name in both context, We have to specifically distinguish between Book Title and Title of a Character. We also have to specifically distinguish between Author Name and Character's Name.
<?php
namespace Book;
const title = "Don Quixote";
const name = "Miguel de Cervantes Saavedra";
namespace Character;
const title = "Sir";
const name = "Sancho Panza";
namespace Test;
echo \Book\title . PHP_EOL;
echo \Book\name . PHP_EOL;
echo \Character\title . PHP_EOL;
echo \Character\name . PHP_EOL;
?>
Here, \Book\title, \Book\name, \Character\title and \Character\name are qualified names.
NOTE: In PHP only four types of code are affected by namespaces: classes, interfaces, functions and constants.
That's that.