Question about instantiateing classes with namespaces

Hey all,

I have started to look into namespaces. I really do like them! I am just having trouble understanding how they are instantiated. before namespaces to start a class I would do something like

$foo = new bar();

but with namespaces I should be able to do something like this correct?

$foo = new \\Core\\bar();

Thanks for the help! I just have to wrap my head around it haha

Check out this thread: link.

I think it answers your qustion.


You may find this helpful:

I like to think of it like a file directory so changing namespace is like changing directory; you can always reference the current directory or an absolute path from the root.

I must admit, I find it easier to just use a fully qualified namespace to access anything outside of the current namespace. It’s a bit of a pain that any use declarations only apply to the current file. For this reason, if you ever had to move a class to a different namespace, it could be a bit of a pain.

If you use the PSR-0 standard instantiating an object would look like:

$object = new \\Vendor\\Package\\Class($foo);

Check out this free video that explains php namespaces quite well: