Jump to content

Do Namespaces exist in PHP?


TomTees

Recommended Posts

Does the concept of a "Namespace" exist in PHP?

 

If so, how do you use them?

 

This is a snippet from ASP.NET that made me ask this question...

 

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.SqlClient" %>

 

TomTees

 

Link to comment
Share on other sites

Namespaces are a .net concept.  With that said, I guess you could use includes to try and get the same result.

 

Namespaces may not be a PHP concept, but they are certainly not just limited to .Net so I wouldn't say "they are a .Net concept".  (Maybe you were just saying that in context?)

 

I believe they are used also in C, C++, Java, and so on.

 

 

 

TomTees

 

 

Link to comment
Share on other sites

Thorpe, it would be entirely possible. However, the changes to the language, and performance impact of such changes, would make using the dot operator out of the question.  Good luck trying to work out whether the aim is to concatenate constants or use some namespaced item!

 

Nightslyr, if your main problem is the syntax being "ugly" then I'd say that's a big win for the implementation! Personally, I quite like the backslash as namespace separator.  :shy:

 

TomTees, as thorpe pointed out, you can use namespaces as of PHP 5.3.0 much like you would with your example in the first post.  The keyword to look for, rather than import is use.

 

Link to comment
Share on other sites

Noticed a LOT of small questions regarding different parts of more advanced PHP from Tom - might I advise you buy yourself a few decent books and read them. You can get the answer to this question within 10 seconds if you search google.

 

Here's a link to another thread regarding good PHP books:

 

http://www.phpfreaks.com/forums/miscellaneous/good-programming-and-web-design-books/

 

Hope that helps.

Link to comment
Share on other sites

Personally, I quite like the backslash as namespace separator.  :shy:

 

I like it too. It promotes the use of directories as a layering mechanism, it will make people start to think about their design when they are creating their n-th directory, and it eases auto-loading (no complex auto-loading schemes and more and more frameworks are adopting it).

Link to comment
Share on other sites

Noticed a LOT of small questions regarding different parts of more advanced PHP from Tom - might I advise you buy yourself a few decent books and read them. You can get the answer to this question within 10 seconds if you search google.

 

Here's a link to another thread regarding good PHP books:

 

http://www.phpfreaks.com/forums/miscellaneous/good-programming-and-web-design-books/

 

Hope that helps.

 

Thanks for the link, but the reason I am here online is becasue books only go so far...

 

 

TomTees

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.