Using namespace in if / else statements

前端 未结 4 621
一个人的身影
一个人的身影 2020-12-09 12:59

I am manipulating the same file to manage two external api classes.

One api class is based on namespaces, the other one is not.

What I would like to do is s

4条回答
  •  情歌与酒
    2020-12-09 13:32

    Use statements should be placed before any executable code (you can have namespaces, classes, functions and constants definitions). Actually it can, just have to be placed unconditionally in some namespace, so no ifs or inside functions. Also don't be afraid of putting use at the top, it does not load any class or instantiate object. it acts only as alias that is used when encountered later during execution.

    As for having them in one file, it is possible to have many namespaces and even global namespace in one file:

    But I strongly discourage such "management" of code. Each class should have it's own file.

提交回复
热议问题