Maybe this is weird question, but I can't work it out what happens internally in php when you write:
use garcha\path\class;
I'm not asking about purpose of namespaces, but about that statement itself, even it does not allocate any memory, I mean, when you even give an alias for some class:
use garcha\path\class as withNewName;
Where is it stored? Or how does it remember the names? Does it happen just on compile time? and not run time? something like just describing the function.
It's not a very complex algorithm (For 5.5, in 5.6 described part for class-names is the same).
as
keyword, this name is used as an alias. Else used last component of imported class name (for XXX\YYY\ZZZ, name would be ZZZ). Convert it to lower case.self
/parent
, it can not be used as name for alias.Where is this table used?
Just during compilation for resolving class names.
There is one more interesting thing about aliases - it has scope as namespace block:
<?php
namespace nsC
{
class classC {}
}
namespace nsA
{
use nsC\classC as importC;
new importC();
}
namespace nsB
{
// ERROR!
new importC(); // or \nsA\importC()
}
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