Define multiple namespaces in the same file

Source: Internet
Author: User

(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 ();
>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.