PHP defines multiple namespaces in the same file

Source: Internet
Author: User
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 ();    }? >
  • 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.