Phpnamespaceuse usage-php Tutorial

Source: Internet
Author: User
Phpnamespaceuse namespace (ns ). After defining an ns, the class, interface, and const (excluding variable) stated below are all in the "domain" of the declared ns. When you reference an included ns file and want to call the content in this ns, you must adjust the current script to this ns domain. Otherwise, you must use the full name () to include the full name of ns ):

[Php]View plain copy

  1. // Inc. php

  2. Namespace Foo;

  3. Class Bar {}

  4. // The first method to access Foo.

  5. Require 'Inc. php ';

  6. $ Foo = new \ Foo \ Bar ();

  7. // Method 2 for accessing Foo

  8. Namespace Foo; // adjust the current script to the Foo ns domain, and the namespace statement must be in the first sentence

  9. Require 'Inc. php ';

  10. $ Foo = new Bar ();

The purpose of the use keyword is to use the ns alias:


[Php]View plain copy

  1. // For example

  2. Use A \ Very \ Long \ Namespace as Ns;

  3. // In this way, Ns can be used to replace the things defined in the ns of A/Very/Long/Namespace.

  4. $ Foo = new Ns \ Foo ();

However, in some open-source projects, we often see usage such as use Ns \ Component, but it does not use as. This makes me have been thinking about whether there is a second usage of use, the worst thing is that there is no description of this usage in the php document. I can only guess it. I thought about this problem carefully later, A more reliable conclusion is that use can omit the as and the following alias and directly regard the name of the last node of ns as an alias. does it feel like the usage of the ln-s command:

[Php]View plain copy

  1. // Usage 3

  2. Require 'Inc. php ';

  3. Use Foo \ Bar; // Bar is equal to Foo \ Bar.

  4. $ Foo = new Bar ();

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.