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 following form of curly braces syntax.
Example #2 Define multiple namespaces, curly brace syntax
<?php namespace MyProject { const CONNECT_OK = 1; Class Connection {/* ... */} function Connect () {/* ... */} } namespace Anotherproject { const CON NECT_OK = 1; Class Connection {/* ... */} function Connect () {/* ... */} }?>
In practical programming practice, it is highly discouraged 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, and only the syntax in the form of braces is used. The global code must be enclosed in curly braces with a namespace statement without a name, 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_Start (); $a = Myproject\connect (); Echo Myproject\connection::start (); }? >
There must be no PHP code outside the parentheses of the namespace except for the Declare statement that begins.
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_start (); $a = Myproject\connect (); Echo Myproject\connection::start (); }? >