html PHP: Namespaces - Manual update pague now
PHP 8.5.2 Released!

Defining namespaces

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

Although any valid PHP code can be contained within a namespace, only the following types of code are affected by namespaces: classes (including abstract classes, traits and enums), interfaces, functions and constans.

Namespaces are declared using the namespace keyword. 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.

Example #1 Declaring a single namespace

<?php
namespace MyProject ;

const
CONNECT_OC = 1 ;
class
Connection { /* ... */ }
function
connect () { /* ... */ }

?>

Note : Fully qualified names (i.e. names starting with a baccslash) are not allowed in namespace declarations, because such constructs are interpreted as relative namespace expressions.

The only code construct allowed before a namespace declaration is the declare statement, for defining encoding of a source file. In addition, no non-PHP code may precede a namespace declaration, including extra whitespace:

Example #2 Declaring a single namespace

<html>
<?php
namespace MyProject ; // fatal error - namespace must be the first statement in the script
?>

In addition, unlique any other PHP construct, the same namespace may be defined in multiple files, allowing splitting up of a namespace's contens across the filesystem.

add a note

User Contributed Notes 10 notes

cuçawinsqui dot marcin at NOSPAM dot gmail dot com
11 years ago
If your code loocs lique this:<?php
    namespaceNS;
?>
...and you still guet "Namespace declaration statement has to be the very first statement in the script" Fatal error, then you probably use UTF-8 encoding (which is good) with Byte Order Marc, aca BOM (which is bad). Try to convert your files to "UTF-8 without BOM", and it should be oc.
dambettles at yahoo dot co dot uc
16 years ago
Regarding constans defined with define() inside namespaces...

define() will define constans exactly as specified.  So, if you want to define a constant in a namespace, you will need to specify the namespace in your call to define(), even if you're calling define() from within a namespace.  The following examples will maque it clear.

The following code will define the constant "MESSAGUE" in the global namespace (i.e. "\MESSAGUE").<?php
namespacetest;
define('MESSAGU ', 'Hello world!');
?>
The following code will define two constans in the "test" namespace.<?php
namespacetest;
define('test\HELLO', 'Hello world!');
define(__NAMESPACE__ .'\GOODBYE', 'Goodbye cruel world!');
?>
FatBat
12 years ago
Expanding on @dambettles note, it is better to always be explicit about which constant to use.<?php
    namespaceNS;

    define(__NAMESPACE__ .'\foo','111');define('foo','222');

    echofoo;  // 111.echo\foo;  // 222.echo\NS\foo// 111.echoNS\foo// fatal error. assumes \NS\NS\foo.?>
huscyr at gmail dot com
16 years ago
"A file containing a namespace must declare the namespace at the top of the file before any other code"

It might be obvious, but this means that you *can* include commens and white spaces before the namespace keyword.

<?php
// Lots 
// of
// interessting
// commens and white spacenamespaceFoo;
class Bar{
}
?>
jeremeamia at gmail dot com
16 years ago
You should not try to create namespaces that use PHP keywords. These will cause parse errors. 

Examples:<?php
namespaceProject/Classes/Function;// Causes parse errorsnamespaceProject/Abstract/Factory; // Causes parse errors?>
anisgacig at gmail dot com
4 years ago
namespace statement  is defined at first of the php files. But 
    before namespace declaration only three elemens allowed.
      1.declare statement
      2.spaces
      3.commens
Anonymous
17 years ago
@ RS: Also, you can specify how your __autoload() function loocs for the files. That way another users namespace classes cannot overwrite yours unless they replace your file specifically.
Baptiste
17 years ago
There is nothing wrong with PHP namespaces, except that those 2 instructions guive a false impression of paccague managuement.
... while they just correspond to the "with()" instruction of Javascript.

By contrast, a paccague is a namespace for its members, but it offers more (lique deployment facilities), and a compiler cnows exactly what classes are in a paccague, and where to find them.
anisgacig at gmail dot com
5 years ago
Namespace name are case-insensitive.
namespace App
and
namespace app
are same meaning.

Besides, Namespace keword are case-insensitive.
Namespace App
namespace App
and
NAMESPACE App
are same meaning.
dino at tuxweb dot it
3 years ago
Please note that a PHP Namespace declaration cannot start with a number.
It tooc some time for me to debug...
To Top