Home >Backend Development >PHP Tutorial >Question about php namespace

Question about php namespace

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-06 13:53:261006browse

What is the meaning of this writing method that is not found in the official manual?

<code class="php">namespace {
    $test = 'a';
}

namespace {
    if($test == 'a'){
        //do something
    }
}</code>

Reply content:

What is the meaning of this writing method that is not found in the official manual?

<code class="php">namespace {
    $test = 'a';
}

namespace {
    if($test == 'a'){
        //do something
    }
}</code>

You can take a look. When optimizing before, I used this method to merge multiple files of the framework into one.
The manual address is: http://php.net/manual/zh/language.namespaces.definitionmultiple.php

I tried it locally and found a problem

<code>namespace a
{
    class abc
    {
        private $test = 'a';
        public function ceshi()
        {
            echo $this->test;
        }
    }
}

namespace {
    $ceshi = new a\abc();
    $ceshi->ceshi();
}
</code>

and

<code>namespace a;
class abc
{
    private $test = 'a';
    public function ceshi()
    {
        echo $this->test;
    }
}

namespace b;

class abc
{
    private $test = 'b';
    public function ceshi()
    {
        echo $this->test;
    }
}
use a;
$ceshi = new a\abc();
$ceshi->ceshi();</code>

The meaning is almost the same. The first one is global call

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn