首页 > php namespace 的疑问

php namespace 的疑问

在官方手册上没有找的这种写法的意思?

namespace {
    $test = 'a';
}

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

我本地自己试了一次,发现一个问题

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

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

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();

意义差不多 第一种是全局调用


可以看看,以前优化的时候,用过这个方式,将框架多个文件合并成一个。
手册地址是: http://php.net/manual/zh/language.namespaces.definitionmultiple.php

【热门文章】
【热门文章】