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

Question about php namespace

WBOY
Release: 2016-07-06 13:53:26
Original
969 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

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>
Copy after login

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

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template