PHP namespace dynamic access and usage tips, namespace_PHP tutorial

Source: Internet
Author: User
Dynamic access to and use of PHP namespaces, namespace. PHP namespace dynamic access and usage skills. namespace of namespacePHP is one of the most important new features added to PHP5.3, in C #, this concept has been used by PHP namespace for dynamic access and usage. 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.

1. dynamically access namespace elements

Namespace me \ poet; function test () {echo '000000';} $ fun = 'test'; // it cannot be used. last $ fun () cannot dynamically Call test (): Fatal error: Call to undefined function test () $ fun = '\ me \ poet \ test '; // correct // $ fun = 'Me \ poet \ test'; // correct $ fun ();

That is to say, dynamic calling must be a limited name or a fully qualified name (concept reference: Use of PHP namespaces)


2. magic constants and operators

Namespace me \ poet; function test () {echo '1';} echo _ NAMESPACE __; // magic constant: namespace name (output me \ poet) // namespace operator: explicitly accesses the elements in the current namespace or sub-namespace. it is equivalent to the self operator \ me \ poet \ test (); namespace \ test () in the class (); // the above two lines of code are equivalent.


III. Alias, import, and global space (including multiple examples)

Namespace ws \ weichen \ www; use ws \ weichen \ www as poet; // define the alias poet // use ws \ weichen \ www; // do not add, then take the last alias (www) function demo () {echo '1';} \ ws \ weichen \ www \ demo (); poet \ demo (); // www \ demo (); // This is called if no as is added

The above three lines of code have the same effect.
Benefits of naming by rules (ws \ weichen \ www): If you change the domain name, you only need to change the prefix name without affecting the use of the alias www in the code below.

/* Import */include 'Hello. class. php '; use \ ws \ weichen \ www; use \ Hello;/* -------------------------------------------------------- * // * supports multiple use statements */use \ nihao \ shijie as hello, \ ws \ weichen \ www;/* ------------------------------------------------------ * // * Global Space: Backslash call */namespace A \ B \ C; // This function is A \ B \ C \ fopen (); function fopen () {$ f = \ fopen('demo.txt '); // call the global fopen function return $ f ;}


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.

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>

Namespaces are the most important new feature added to PHP 5.3, this concept has been...

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.