PHP namespaces, PHP namespace Usage _php Tutorial

Source: Internet
Author: User

PHP namespaces, PHP namespace usage


/* First the namespace exists in order to resolve a naming conflict problem that refers to a non-homogeneous type */

/*1. Define Namespaces */
namespace My;
Class a{};

namespace Your;
Class a{};


/*2. Referencing a namespace in the same project */
namespace My;
Class a{};
Direct references (with unqualified names)
$a = new A ();

Referencing child namespaces (using qualified names)
$obj = new Son\a ();
namespace My\son;
Class a{};

Namespace (fully qualified name) that references the beginning of the root directory
$b =namespace\my\a ();

/*3. Using use to take aliases and references (import) */

namespace My\one\two;
Class a{};

namespace Your;
Class a{};


Use \my\one\two//If the end is a namespace, the last segment is aliased by default, similar to adding environment variables

$obj =new two\a ();
$obj =new \my\one\two\a ();


Use \my\one\two\a as MyA//If the end is a class name then the class name is equivalent to a shortcut

$obj =new MyA ();

http://www.bkjia.com/PHPjc/1095473.html www.bkjia.com true http://www.bkjia.com/PHPjc/1095473.html techarticle php namespace, PHP namespace usage/* First ~ The namespace exists to resolve the issue of naming conflicts in a non-homogeneous type *//*1. Defining namespaces */namespace My; class a{}; na ...

  • 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.