An analysis of PHP namespaces

Source: Internet
Author: User

Tag: Specify the meaning character in advance to return the name IMP color manual

Introduced

The impression is only Java code will use a lot of import, at the beginning to see the face of a crazy and to PHP heart proud: or My big php cool enough concise, but PHP also has a namespace this said, these years with more and more. So why bother? Have to write a lot of use (God annoying ...) A face helpless), the PHP manual gives the standard answer:

In PHP, namespaces are used to solve two types of problems encountered when writing a class library or application to create reusable code such as classes or functions:

    1. User-written code conflicts with the name of a class/function/constant or third-party class/function/constant inside PHP
    2. Creates an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem), improving the readability of the source code.

Well, for two dogs to understand, that is:

    1. Resolving naming conflicts
    2. Renaming

In fact, it is a meaning: with it can be more freely named, without fear of conflict

Give me a chestnut.
<?phpnamespace my;//define Namespaces
Overwrite PHP classclassmysqli { Public functionquery () {return1; }}
Overwrite PHP functionsfunction Preg_replace_callback() { return2;}
Overwrite PHP constantsConstPHP_SAPI = 3;$a=Newmysqli ();Var_dump($a-query ());$b=Preg_replace_callback();Var_dump($b);Var_dump(PHP_SAPI);?>

We can see that we have duly returned to:

int(1) int(2) int(3)

So here's the question, what do I do with PHP mysqli now? The front plus \ is good:

$a new \mysqli;

The most common scenario we encounter in a project is a conflict caused by two class libraries or methods with the same name. Suppose there is a A, a, two third-party class libraries, which have the cache class, and I want to use both of them:

├─application
│├─a
││├─cache.php
│├─b
││├─cache.php
│├─test.php

a/cache.php:

<? phpnamespace A; class cache{    function  set () {        return ' OK ';    }}? >

b/cache.php:

<? phpnamespace B; class cache{    function  set () {        return ' success ';    }}? >

test.php:

<? PHP require ' a/cache.php '; require ' b/cache.php '; $cache New A\cache (); Var_dump ($cache,set ()); $cache New B\cache (); Var_dump ($cache,set ());? >

Return:

string(2) "ok" string(7) "success"

You can see that as long as his two namespaces are different, they can be called correctly to the

NAMESPACE and __namespace__

__NAMESPACE__ returns the current namespace string, and the NAMESPACE keyword can be used to explicitly access elements in the current namespace or child namespace

$classname = __namespace__. ' \mysqli '; $a New $classname (); Var_dump ($a,query); $a New namespace\mysqli (); Var_dump ($a->query ());

Use

The Use keyword is used to specify which namespace is used, the above example we are not using the use is because we specify the path when new, so much trouble, test.php changed to use using:

 <? php  use   A\cache;  require  ' a/cache.php '  require  ' b/cache.php '   $cache  = new  cache (); // new A\cache  var_dump  ( $cache ->set ()); 
$cache = new B\cache (); New B\cache var_dump ( $cache - >set ()); ?

In this way, each time the new cache is instantiated by default A\cache, and can go back early and female tickets to drill bedding ~

Use as can specify aliases, and when a class library namespace is long, you can steal lazy as a short name, considering the class library code as follows:

<? phpnamespace Blah\blah\blah; class cachesomethingimportingandverydangerous{    function  set () {        return ' Success ';    }}? >

God, this long method name, the whole person is not good, use as a bit, the whole world is quiet:

<? PHP  Use  as Cache; require ' b/cachesomethingimportingandverydangerous.php '; $cache New Cache (); Var_dump ($cache,set ());? >

Above! Wish you a happy New Year in advance!

An analysis of PHP namespaces

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.