I just wonder if there's a way to make a class behave as if it is in global namespace using a "use" keyword, so this class would behave as namespaced class only from outside of the class. Something like:
namespace wherever\somewhere\deep\deep\inside;
use \; // root namespace.. note: this doesn't work
class stuff{
//....
}
anyone?
Using the global namespace won't work the way you expect.
By default, you can reference a globally namespaced class by adding a backslash -- eg $x = new \PDO(...);
. Trying to use \
won't change that.
If you want to drop the backslash from globally namespaced classes, you need to use
each of them specifically. In namespaced PHP, any class reference that doesn't have a namespace is assumed to be in the current namespace, unless it is explicitly referenced by a use
statement.
eg:
use \PDO, \SplFileObject;
now we can call new PDO(...)
or new SplFileObject()
without the backslash. But other global classes that aren't in the use
would still need the backslash.
You can access the global namespace if you use brackets like this:
namespace My\Space {
function myScopedFunction() { .. }
}
namespace {
function myGlobalFunction() { .. }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With