Dynamic access and usage techniques for PHP namespaces (namespace) _php instance

Source: Internet
Author: User

The PHP namespace (namespace) is one of the most important new features in PHP 5.3, a concept that has been in C # already, and namespace in PHP is the same as the C # concept.

I. Elements that dynamically access the namespace

namespace Me\poet;
function test ()
{
  echo ' 1111 ';
}
$fun = ' test ';//cannot be used so that the last $fun () cannot dynamically invoke to test (): Fatal error:call to undefined function test ()
$fun = ' \me\poet\test '; Correct
//$fun = ' me\poet\test ';//correct
$fun ();

That is, a dynamic call must be a qualified name or a fully qualified name (conceptual reference: the basis for using the PHP namespace)


Two. Magic Constants and operators

namespace Me\poet;
function test ()
{
  echo ' 1 ';
}
Echo __namespace__; Magic Constants: Namespace name (output Me\poet)
//namespace operator: explicitly accessing elements in the current namespace or child namespace, equivalent to the self operator
\me\poet\test () in the class;
Namespace\test ();
The two lines of code are equivalent.


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

namespace Ws\weichen\www;
Use Ws\weichen\www as poet;//to define alias poet
//use ws\weichen\www;//without as, then take the last alias (WWW)
function demo ()
{
  echo ' 1 ';
}
\ws\weichen\www\demo ();
Poet\demo ();
Www\demo (); Does not add as, then this calls the

The above three lines of code have the same effect.
The advantage of naming by rule (ws\weichen\www) is that if you change the domain name, you can change the prefix name so that it does not affect the use of the alias www in the following code.

/* 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 Global fopen functions return
  $f;
}

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.