The usage basics and examples of the PHP namespace, namespace_PHP tutorial

Source: Internet
Author: User
The usage basis and example of the PHP namespace, namespace. The usage basis and example of the PHP namespace, namespace of namespacePHP is one of the most important new features added in PHP5.3, in C #, this concept is already used in PHP namespace.

The namespace of PHP is one of the most important new features added to PHP 5.3. this concept has been available in C # for a long time, the namespace in php is actually the same as that in c.

I. PHP namespace mainly solves three types of conflicts: constants, functions, and classes.

In general, namespace is equivalent to creating a directory. the code under namespace is put in this directory, which is differentiated from that outside.

/* | --------------------------------- | Namespace example | @ Blackeye poet
 
  
| ------------------------------- */Namespace myself; function var_dump () {echo 100;} var_dump (); // call a custom function (relative path) \ myself \ var_dump (); // call a custom function (absolute path) \ var_dump (100); // call a global function)
 

Note: No code exists before namespace, except declare (). multiple files can use the same namespace, but the content defined under the same namespace cannot conflict. Namespace supports sub-namespaces, such as namespace \ myself \ good, which is equivalent to the concept of multi-level directories.

2. multiple namespaces exist in the same file.

1.

/*** If multiple namespaces are used in the same file, use the formula */namespace nihao \ shijie; function demo (){//.......} namespace hello \ world; function test (){//........} \ nihao \ shijie \ demo (); \ hello \ world \ test ();

2.

/*** If multiple namespaces are used in the same file, we recommend that you expand the braces */namespace nihao \ shijie {function test_one () {//......};} namespace hello \ world {function test_two (){//........}} \ nihao \ shijie \ test_one (); \ hello \ world \ test_two ();

Multiple namespaces are used in the same file. They are mainly used in projects to merge multiple PHP scripts into the same file. In practice, they are not recommended!

III. name resolution rules (several concepts)

1. unrestricted name: the name does not contain a namespace separator, for example, myself.

2. limited name: the name contains namespace delimiters, such as nihao \ shijie.

3. fully qualified name: the name contains a delimiter and starts with a namespace delimiter, for example, \ nihao \ shijie (the concept of absolute path)


What is php use php namespace?

1. namespace Zend \ Http \ PhpEnvironment;

This code defines a namespace, which can be understood as a domain name named Zend \ Http \ PhpEnvironment.

After definition, the classes, interfaces, and const stated below are all in the declared "domain. When you reference a namespace inclusion file and want to call the content in it, you must:

Adjust the current script to this domain name. Otherwise, you must use the full name of namesapce.

For example, the inc. php file:

Namespace Zend \ Http \ PhpEnvironment;
Class Bar {} // defines a class

When other files are called:

// The first method to access Foo.
Require 'Inc. php ';
$ Foo = new \ Zend \ Http \ PhpEnvironment \ Bar ();

// Method 2 for accessing Foo
Namespace Foo; // adjust the current script to the Foo ns domain, and the namespace statement must be in the first sentence
Require 'Inc. php ';
$ Foo = new Bar ();

2. the use keyword aims to use the ns alias:

For example

// The first method to access Foo.
Require 'Inc. php ';
$ Foo = new \ Zend \ Http \ PhpEnvironment \ Bar ();

After using uses, write as follows:

Use \ Zend \ Http \ PhpEnvironment as pe; // defines the alias

$ Foo = new \ pe \ Bar (); // use a short alias to replace the original

If the "as..." section is omitted, you can directly use the text in the last section instead. for example:

Use \ Zend \ Http \ PhpEnvironment; // defines the alias
$ Foo = new \ PhpEnvironment \ Bar (); // use a short alias to replace the original

========================================================== ==========

Php official manual:

In PHP, the namespace is used to solve the two problems encountered when you create reusable code such as classes or functions when writing class libraries or applications:

1. the user-written code conflicts with the names of PHP internal classes/functions/constants or third-party classes/functions/constants.
2. create an alias (or short) name for a long identifier name (usually defined to mitigate the first type of problem) to improve the readability of the source code.

The PHP namespace provides a way to combine related classes, functions, and constants.

The PHP namespace supports two alias or import methods: use an alias for the class name or namespace name, and use the alias through the use operator .... Remaining full text>

Php namespace error?

No syntax issues.

What is your PHP version? PHP versions later than 5.3.0 support namespaces. Your PHP version may be relatively low.

Namespace is the most important new feature added to PHP 5.3, this concept is already in C...

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.