When using PHP
namespaces, do you use them like:
\Foo\Bar->method()
or
Foo\Bar->method()
Notice the difference is only a leading slash. Or are both valid, and mean different things?
Thanks.
The usage of a leading \
is like absolute and relative paths in filesystems. Best explained with code:
namespace test;
$dt = new DateTime();
fails, as we are using an relative path (without the leading \
), and the current namespace test
. And there is not class DateTime
in this fictional namespace.
namespace test;
$dt = new \DateTime();
works as we are using an absolute namespaces path. As DateTime
is in the global namespace \
it will be found.
Find more info in the PHP manual about namespaces
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