Those tutorials don't have the php3

Source: Internet
Author: User
Php.net
(PHP 5 >= 5.3.0, PHP 7)

Define namespaces

Although any valid PHP code can be included in a namespace, only the following types of code are affected by namespaces: classes (including abstract classes and traits), interfaces, functions, and constants.
If a file contains a namespace, it must declare the namespace before all other code, except one: the DECLARE keyword. All non-PHP code, including whitespace characters, cannot appear before the declaration of the namespace.

 
  

The same namespace can be defined in multiple files, which allows the content of the same namespace to be split into separate files.

Defining child namespaces

 
  

Define multiple namespaces in the same file

Note: In practical programming practice, it is not recommended to define multiple namespaces in the same file!

 
  

Using namespaces

Three ways to refer to the class name:

Unqualified names (identifiers that do not contain namespace separators in the name), or class names that do not contain prefixes. For example $a =new foo (); or Foo::staticmethod ();. If the current namespace is Currentnamespace,foo, it will be resolved to Currentnamespace\foo. If the code that uses Foo is global and does not contain code in any namespace, Foo is parsed as Foo. * * WARNING * *: If a function or constant in a namespace is undefined, the unqualified function name or constant name is resolved to the global function name or constant name.

The qualified name (the identifier with the namespace delimiter in the name), or the name that contains the prefix, for example $a = new Subnamespace\foo (); or Subnamespace\foo::staticmethod ();. If the current namespace is Currentnamespace, Foo will be parsed to Currentnamespace\subnamespace\foo. If the code that uses Foo is global and does not contain code in any namespace, Foo is parsed as Subnamespace\foo.

The fully qualified name (the identifier that contains the namespace delimiter in the name and begins with the namespace delimiter), or contains the name of the global prefix operator. To access any global class, function, or constant, you can use a fully qualified name, such as \strlen () or \exception.

Tips

$a = ' \namespacename\classname '; $obj = new $a; $a = ' namespacename\classname '; $obj = new $a; $b = ' namespacename\funcname '; $b (); Prints namespacename\funcname$b = ' \namespacename\funcname '; $b (); Also prints Namespacename\funcname

Working with related constants

The value of the constant __namespace__ is a string containing the name of the current namespace. In the global, not included in any namespace code, it contains an empty string.

Using namespaces: Aliases/imports

 
  

The class name always resolves to the name in the current namespace. Therefore, you must use a fully qualified name when accessing the name of a class that is inside the system or that is not included in the namespace.

  • Related Article

    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.