PHP NamespacesNamespaces are qualifiers that solve two different problems: - They allow for better organization by grouping classes that work together to perform a task
- They allow the same name to be used for more than one class
For example, you may have a set of classes which describe an HTML table, such as Table, Row and Cell while also having another set of classes to describe furniture, such as Table, Chair and Bed.
Namespaces can be used to organize the classes into two different groups while also preventing the two classes Table and Table from being mixed up.
Declaring a NamespaceNamespaces are declared at the beginning of a file using the namespace keyword: SyntaxDeclare a namespace called Html: <?php namespace Html; ?> Note: A namespace declaration must be the first thing in the PHP file. The following code would
be invalid: <?php echo "Hello World!"; namespace Html; ... ?> Constants, classes and functions declared in this file will belong to the Html namespace: ExampleCreate a Table class in the Html namespace: <?php namespace Html; class Table { public $title = ""; public $numRows = 0; public function message() { echo "<p>Table '{$this->title}'
has {$this->numRows} rows.</p>"; } } $table = new Table(); $table->title = "My table"; $table->numRows = 5; ?> <!DOCTYPE html> <html> <body> <?php $table->message(); ?> </body> </html> Try it Yourself » For further organization, it is possible to have
nested namespaces: SyntaxDeclare a namespace called Html inside a namespace called Code: <?php namespace Code\Html; ?>
Using NamespacesAny code that follows a namespace declaration is operating inside the namespace, so classes that belong to the namespace can be instantiated without any qualifiers. To access classes from outside a namespace, the class needs to have the namespace attached to it. ExampleUse
classes from the Html namespace: <?php $table = new Html\Table() $row = new Html\Row(); ?> Try it Yourself » When many classes from the same namespace are being used at the same time, it is easier to use the namespace keyword: ExampleUse classes from the Html namespace without the need for the Html\qualifier: <?php namespace
Html; $table = new Table(); $row = new Row(); ?> Try it Yourself »
Namespace AliasIt can be useful to give a namespace or class an alias to make it easier to write. This is done with the use keyword: ExampleGive a namespace an alias: <?php use Html as H; $table = new H\Table(); ?> Try it Yourself » ExampleGive a class an alias: <?php use Html\Table as T; $table = new T(); ?> Try it Yourself » (PHP 5 >= 5.3.0, PHP 7, PHP 8) Before discussing the use of namespaces, it is important to understand how PHP knows which namespaced element your code is requesting. A simple analogy can be made between PHP namespaces and a filesystem. There are three ways to access a file in a file system: - Relative file name like
foo.txt . This resolves to currentdirectory/foo.txt where currentdirectory is the directory
currently occupied. So if the current directory is /home/foo , the name resolves to /home/foo/foo.txt . - Relative path name like
subdirectory/foo.txt . This resolves to currentdirectory/subdirectory/foo.txt . - Absolute path name like
/main/foo.txt . This resolves to /main/foo.txt .
The same principle can be applied to namespaced elements in PHP. For example, a class name can be referred to in three ways: - Unqualified name, or an unprefixed class name like
$a = new foo(); or
foo::staticmethod(); . If the current namespace is currentnamespace , this resolves to currentnamespace\foo . If the code is global, non-namespaced code, this resolves to foo . One caveat: unqualified names for functions and constants will resolve to global functions and constants if the namespaced function or constant is not defined. See Using namespaces: fallback to global function/constant for details.
- Qualified name, or a prefixed class name like
$a = new subnamespace\foo(); or subnamespace\foo::staticmethod(); . If the current namespace is currentnamespace , this resolves to currentnamespace\subnamespace\foo . If the code is global, non-namespaced code, this resolves to subnamespace\foo . - Fully qualified name, or a prefixed name with global prefix operator like
$a = new \currentnamespace\foo(); or \currentnamespace\foo::staticmethod(); . This always resolves to the literal name specified in the code, currentnamespace\foo .
Here is an example of the three kinds of syntax in actual code:
file1.php
<?php namespace Foo\Bar\subnamespace;
const
FOO = 1; function foo() {} class foo { static function staticmethod() {} } ?>
file2.php
<?php namespace Foo\Bar; include 'file1.php';
const
FOO = 2; function foo() {} class foo { static function staticmethod() {} }/* Unqualified name */ foo(); // resolves to function Foo\Bar\foo foo::staticmethod(); // resolves to class Foo\Bar\foo, method staticmethod echo FOO; // resolves to constant Foo\Bar\FOO/* Qualified name */ subnamespace\foo(); // resolves to function Foo\Bar\subnamespace\foo subnamespace\foo::staticmethod(); // resolves to class Foo\Bar\subnamespace\foo, // method staticmethod echo subnamespace\FOO; // resolves to constant Foo\Bar\subnamespace\FOO /* Fully qualified name */ \Foo\Bar\foo(); // resolves to function Foo\Bar\foo \Foo\Bar\foo::staticmethod(); // resolves to class Foo\Bar\foo, method staticmethod echo \Foo\Bar\FOO; // resolves to constant Foo\Bar\FOO ?>
Note that to access any global class, function or constant, a fully qualified name can be used, such as \strlen() or \Exception or \INI_ALL . Example #1 Accessing global classes, functions and constants from within a namespace
<?php namespace Foo;
function
strlen() {} const INI_ALL = 3; class Exception {}$a = \strlen('hi'); // calls global function strlen $b = \INI_ALL; // accesses global constant INI_ALL $c = new \Exception('error'); // instantiates global class Exception ?>
richard at richard-sumilang dot com ¶ 14 years ago
Syntax for extending classes in namespaces is still the same.
Lets call this Object.php:
<?php
namespace com\rsumilang\common;
class
Object{
// ... code ...
}
?>
And now lets create a class called String that extends object in String.php:
<?php
class String extends com\rsumilang\common\Object{
// ... code ...
}
?>
Now if you class String was defined in the same namespace as Object then you don't have to specify a full namespace path:
<?php
namespace com\rsumilang\common;
class String extends Object
{
// ... code ...
}
?>
Lastly, you can also alias a namespace name to use a shorter name for the class you are extending incase your class is in seperate namespace:
<?php
namespace com\rsumilang\util;
use com\rsumlang\common as Common;
class String extends Common\Object
{
// ... code ...
}
?>
- Richard Sumilang
Anonymous ¶ 7 years ago
<?phpnamespace Foo;
try {
// Something awful here // That will throw a new exception from SPL } catch (Exception as $ex) { // We will never get here // This is because we are catchin Foo\Exception } ?>
Instead use fully qualified name for the exception to catch it<?php namespace Foo; try { // something awful here // That will throw a new exception from SPL } catch (\Exception as $ex) { // Now we can get here at last } ?>
Lukas Z ¶ 10
years ago
Well variables inside namespaces do not override others since variables are never affected by namespace but always global: "Although any valid PHP code can be contained within a namespace, only four types of code are affected by namespaces: classes, interfaces, functions and constants. "
Source: "Defining Namespaces" http://www.php.net/manual/en/language.namespaces.definition.php
tom at tomwardrop dot com ¶ 10 years ago
It seems the file system analogy only goes so far. One thing that's missing that would be very useful is relative navigation up the namespace chain, e.g.
<?php namespace MyProject { class Person {} }
namespace
MyProject\People { class Adult extends ..\Person {} } ?>
That would be really nice, especially if you had really deep namespaces. It would save you having to type out the full namespace just to reference a resource one level up.
philip dot preisser at arcor dot de ¶ 11 years ago
Working with variables can overwrite equal variables in other namespaces
<?php // php5 - package-version : 5.3.5-1ubuntu7.2namespace main {}
namespace
main\sub1 { $data = 1; } namespace main\sub2 { echo $data;// 1 $data = 2; } namespace main\sub1 { echo $data;// 2 $data = 1; } namespace { echo $data;// 1 }?>
How does PHP namespace work?
Like C++, PHP Namespaces are the way of encapsulating items so that same names can be reused without name conflicts. It can be seen as an abstract concept in many places. It allows redeclaring the same functions/classes/interfaces/constant functions in the separate namespace without getting the fatal error.
Should I use namespace PHP?
The main objective of namespaces is to prevent name collisions, more over they are used to group classes, methods. As you mentioned there are a lot of classes within a Laravel project so namespaces would have to be used to prevent collisions which will happen in big projects.
How can use namespace in another file in PHP?
A file containing a namespace must declare the namespace at the top of the file before any other code - with one exception: the declare keyword. Add this to your included file as well. namespace my_ns; After that, your code works just fine.
Can I use two namespace in PHP?
Defining multiple namespaces in the same file ¶
Multiple namespaces may also be declared in the same file. There are two allowed syntaxes. This syntax is not recommended for combining namespaces into a single file. Instead it is recommended to use the alternate bracketed syntax.
|