How to implement nested namespace in PHP?

时光总嘲笑我的痴心妄想 提交于 2019-12-31 03:02:43

问题


use level1\level2\level3;

Can someone explain with a simple demo ?


回答1:


To clear up any confusion regarding different syntax use, namespaces support only two syntaxes, either bracketed or simple-combination both will work. I suggest if you use one over the other, be consistent.

<?php
namespace my\stuff\nested {  // <- bracketed syntax
 class foo {}
}
?>

It creates a class foo inside of the nested namespace with bracketed syntax ({}), it is equivalent to

<?php
namespace my\stuff {  // bracketed syntax but with a nested look
  namespace nested {
     class foo {}
  }
}
?>

You can also use nested namespaces with simple-combination syntax (;)

<?php
namespace mine;
use ultra\long\ns\name;  // <- simple-combination syntax

$a = name\CONSTANT;
name\func();
?>

PHP: FAQ: things you need to know about namespaces



来源:https://stackoverflow.com/questions/2230025/how-to-implement-nested-namespace-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!