(PHP 5 >= 5.3.0, PHP 7)
You can also define multiple namespaces in the same file. There are two syntactic forms of defining multiple namespaces in the same file.
Example #1 Define multiple namespaces, simple combination syntax
<?php
namespace MyProject;
Const CONNECT_OK = 1;
Class Connection {/* ... */}
function Connect () {//// }
namespace Anotherproject;
Const CONNECT_OK = 1;
Class Connection {/* ... */}
function Connect () {/* ... */ }
?>
It is not recommended to use this syntax to define multiple namespaces in a single file. It is recommended that you use the syntax in the following curly brace form.
Example #2 Define multiple namespaces, curly braces syntax
<?php
namespace MyProject {
const CONNECT_OK = 1;
Class Connection {/* ... */}
function Connect () {/* ... */ }
}
namespace Anotherproject {
cons t CONNECT_OK = 1;
Class Connection {/* ... */}
function Connect () {/* ... *}
}
?>
In practical programming practice, it is not recommended to define multiple namespaces in the same file. This approach is primarily used to merge multiple PHP scripts into the same file.
The code in the global non-namespace is combined with the code in the namespace to use only the syntax in curly braces. The global code must be surrounded by a namespace statement with no name and braces, for example:
Example #3 define multiple namespaces and code that is not included in the namespace
<?php
namespace MyProject {
const CONNECT_OK = 1;
Class Connection {/* ...
/} function connect () {/* ... */}
}
namespace {//Global code
session_s Tart ();
$a = Myproject\connect ();
Echo Myproject\connection::start ();
>
In addition to the Declare statement that begins, there must be no PHP code outside the parentheses of the namespace.
Example #4 define multiple namespaces and code that is not included in the namespace
<?php
Declare (encoding= ' UTF-8 ');
namespace MyProject {
const CONNECT_OK = 1;
Class Connection {/* ... */}
function Connect () {/* ... * /}
}
namespace {//Global code
Session_sta RT ();
$a = Myproject\connect ();
Echo Myproject\connection::start ();
>