Importing variable namespaces

后端 未结 2 1879
时光取名叫无心
时光取名叫无心 2021-01-06 17:28

Would it be possible to import namespaces using a variable like this:

$namespace = \'User\\Authorization\\Certificate\';
use $namespace;

Ob

2条回答
  •  温柔的废话
    2021-01-06 17:48

    No, PHP expects the use to follow a namespace, not an expression (which includes not a constant). See Using namespaces: Aliasing/Importing.

    However if you change the PHP code on the fly before executing it in some kind of aggregation or compilation phase within your system, you could replace the text with some variable data which looks static enough for PHP then.

    But I have no idea if your system is capable of doing so. I could imagine some stream wrapper or stream filter doing this on the fly. It could transparently take care of inserting the variable namespace name.

提交回复
热议问题