简体   繁体   中英

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. 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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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