简体   繁体   English

如何在php中使用函数和变量定义命名空间?

[英]How to define a namespace using function and variable in php?

This is actually a short question. 这实际上是一个简短的问题。 Let's say I use namespaces in my php script. 假设我在php脚本中使用名称空间。 So I place this at the beginning of my file. 所以我把它放在我的文件的开头。

namespace Project\Module\Header_text_button\template;

Now what I want is to set this "Header_text_button" part dynamically, something like (completely demonstration): 现在我想要的是动态设置这个“Header_text_button”部分,如(完全演示):

namespace App\Modules\".$variable."\Controllers;

or 要么

namespace Project\Module\.basename(basename(__DIR__)).\template;

How can I do such a thing, is this possible at all? 我怎么能做这样的事情,这有可能吗?

Yes as result it is NOT possible. 是的结果是不可能的。 It is executed at runtime before all the other stuff even was even created. 它甚至在创建所有其他内容之前在运行时执行。 besides it has to be defined at the very top of the script so no other variables or dynamic stuff can't be created before. 此外,它必须在脚本的最顶部定义,因此之前不能创建其他变量或动态内容。

Php manual for namespace definition 命名空间定义的Php手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM