SitePoint Sponsor

User Tag List

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

    Dynamic fully qualified namespaces - good or bad?

    I'm quite new to namespaces though I am now sold on the idea and think they're excellent. My main gripe was though that the scope of a use statement was only in the file it was called. So, in my current project outside of the bootstrap file I have loads of fully qualified namespaces. E.g.

    PHP Code:
    $object = new \Vendor\Package\Class($parameter); 
    Not that it would happen often but if I renamed the Package to Package2 I would have to go through my code and rename \Vendor\Package\ to \Vendor\Package2\.

    I have just found out as of 5.3 (which I'm using) you can call classes through variables—including namespaces. So I guess I could do something like:

    PHP Code:
    $package '\Vendor\Package\\';
    $currentPackage $package 'Class';
    $object = new $currentPackage($parameter); 
    I'm interested to know if you think this is a good idea or not. My gut tells me to avoid this for readability but was interested to know if others were using this. Of course, if you ever renamed a class you'd be stuck updating everything anyway.

  2. #2
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    701
    Mentioned
    4 Post(s)
    Tagged
    1 Thread(s)
    IMHO opinion php namespace should be used they way they were intended to be and there is not much point in trying to get them work in some other fashion. Just makes your code more confusing and slower.

    In most cases, just add a use statement at the top of each file for all the objects you need. Should only be a few and then use just the class name in your code.

    PHP Code:
    use \Vendor\Package\SomeClass
    ...
    $someClass = new SomeClass(); 
    If you do end up with a big refactor job then at least you only need to change the user statements.

    Consider also using composer. http://getcomposer.org/doc/00-intro.md
    Even if you are not yet using any third party libraries composer will give you a nice autoloader for free so you won't need things like \Vendors. Your namespace's will be shorter and more flexible.

  3. #3
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    441
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ahundiak View Post
    IMHO opinion php namespace should be used they way they were intended to be and there is not much point in trying to get them work in some other fashion. Just makes your code more confusing and slower.

    In most cases, just add a use statement at the top of each file for all the objects you need. Should only be a few and then use just the class name in your code.

    PHP Code:
    use \Vendor\Package\SomeClass
    ...
    $someClass = new SomeClass(); 
    If you do end up with a big refactor job then at least you only need to change the user statements.

    Consider also using composer. http://getcomposer.org/doc/00-intro.md
    Even if you are not yet using any third party libraries composer will give you a nice autoloader for free so you won't need things like \Vendors. Your namespace's will be shorter and more flexible.
    Thanks for the link - great reply!


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
  •