cmarenburg — 2013-07-24T11:42:28-04:00 — #1
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
mjpr — 2013-07-24T12:15:59-04:00 — #2
Check out this thread: link.
I think it answers your qustion.
drquincy — 2013-07-24T12:16:29-04:00 — #3
You may find this helpful: http://php.net/manual/en/language.namespaces.rules.php
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);
system — 2013-07-26T11:42:12-04:00 — #4
Check out this free video that explains php namespaces quite well: http://knpuniversity.com/screencast/php-namespaces-in-120-seconds