SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    436
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Help understanding PHP namespaces

    I'm trying to use namespaces and have got them working but I am a little confused. Am I right in saying the advantages are:

    1. Auto load classes
    2. Helps keep classes organised
    3. Prevents class name clashes

    Correct? Here's where I'm confused:

    1. Why can't use declare namespace aliases once in a file (e.g. namespaces.php) and then the aliases still apply in any files that include that file? Do I really have to write use or use fully-qualified namespaces on each file? Am I missing something? Is it because namespaces should never change once set?
    2. I am using to PSR-0 standard so I have all my namespaces like {Company}\{Package} Is that a good way of doing it? So is the idea if i am Microsoft and I am using namespaces so long as I start the namespaces with Microsoft I know I will never clash with anyone else? (Unless of course they steal my company name )
    3. Is the idea if you want to use another library that uses namespaces you can simply copy a folder into your source?

    Thanks.

  2. #2
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    1. Yes, you basically need to add a Use statement for each class or use FQNs.
    2. Yes, starting your classes with some unique domain is generally a good idea.
    3. Take a look at php composer. It's designed around the notion of being able to use libraries from multiple sources. It includes support for auto loading. Better than copying.

    Consider taking a look at some projects that use namespaces. Doctrine 2, Symfony 2, Silex etc.

    Namespaces can make your code easier to read and maintain by listing classes used in given file along the top (use statements) and then allowing for shorter class names (aliases). But stick to using them the way the php folks intended them to be used. Lots of people get into trouble trying to apply namespace concepts from other languages.

  3. #3
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    436
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I do like the concept of namespaces and am going to stick with it. Although I don't like the look of \ in my code I do like the autoloading.

    So, is it a given that once you set a namespace it never changes? My main gripe as a project gets bigger you may wish to split packages—or it may turn out a class is better in a different package. It makes it harder to update the code.

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,411
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    It's worth mentioning that it's not necessary to use namespaces if you want to use autoloading.

  5. #5
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    436
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    It's worth mentioning that it's not necessary to use namespaces if you want to use autoloading.
    Thanks, yes. I should have said the other good reason is, of course, I know my Database class won't class with anyone else's.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •