Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    New Coder
    Join Date
    Jun 2018
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Namespacing: what's the benefit?

    New to namespacing.

    I am looking at a project where I am using Dependency Injection with constructor injection. So what would be the benefit of namespacing my code, apart from avoiding clashes in class names with external libraries?

    1. I basically never reference any function or method outside of a class, it's all pretty much encapsulated and self-contained (due to DI)
    2. I simply use a prefix pci_ for my class names to avoid any conflicts.

    Here's a typical class in my project:

    PHP Code:
    if( !class_exists'pci_WPclient' ) ) {

        class 
    pci_WPclient extends pci_model implements pci_ClientInterface {

            public 
    $WP_User;
            public 
    $project;
            private 
    $session;

            public function 
    __constructpci_session $sessionpci_WPproject $project ) {

                
    $this->session $session;
                
    $this->project $project;
            }

            public function 
    get_object() {

                if( !isset( 
    $this->session->pci_project ) )
                    
    pci_WPerror::$_obj->quit2'No project found to load client object' );

                if( !isset( 
    $this->session->pci_client ) ) {
                    
    $client get_post_meta$this->project->get_ID(), 'pci_project_client'TRUE );

                    
    $client_id = ( is_array$client ) ) ? $client'ID' ] : intval$client );
                } else {

                    
    $client_id $this->session->pci_client;
                }

                
    $this->WP_User get_user_by'ID'$client_id );
                
    $this->session->pci_client $this->WP_User->ID;
            }

            public function 
    get_ID() {

                if( ! isset( 
    $this->session->pci_client ) )
                    
    $this->get_object();

                return 
    $this->session->pci_client;
            }

            public function 
    get_fullname() {

                if( !
    $this->WP_User instanceof WP_User )
                    
    $this->get_object();

                return 
    $this->WP_User->data->display_name;
            }

            public function 
    get_avatar() {
                
            }

            public function 
    get_email() {

                if( !
    $this->WP_User instanceof WP_User )
                    
    $this->get_object();

                return 
    $this->WP_User->data->email;
            }

        }


    I fail to see the benefits of namespacing for classes like this, it just seems like an additional hassle for very little benefit. Am I missing something?

  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,651
    Thanks
    24
    Thanked 580 Times in 573 Posts
    I fail to see the benefits of namespacing for classes like this
    although you implicitly use the namespaces pci an WP ...

    Also mind that the user object is injected through a global dependency ( get_user_by() ), not by DI.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  3. #3
    New Coder
    Join Date
    Jun 2018
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    although you implicitly use the namespaces pci an WP ...
    Okay, I need to let that sink in...


    Quote Originally Posted by Dormilich View Post
    Also mind that the user object is injected through a global dependency ( get_user_by() ), not by DI.
    Yes; methods should only access the user object through its interface functions, not the properties. I guess I should make that $WP_User property private and see where the app fails...

  4. #4
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    2,875
    Thanks
    3
    Thanked 411 Times in 400 Posts
    To me namespaces always came across as REDUNDANT to objects and just plain knowing how to write code... ANOTHER reason (on the massive laundry list of reasons) I'm not a big fan of turdpress is they willy nilly mix-and-match methodologies.

    But I come from a Wirth language / smalltalk background when it comes to objects, so I look at things RADICALLY differently from how people coming from a C syntax mindset do.

    I really never grasped why PHP had the whole namespace nonsense in the first place, much less at the same time as objects. Seems like just another pointless redundancy... like how mysqli should really bite the dust; or how they should remove all the remaining pointless redundancies like exit/die. PARTICULARLY since being an interpreted language means each extra command we don't need -- even if you don't call it -- just adds overhead to the parser.
    Last edited by deathshadow; Jun 14th, 2018 at 02:01 AM.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  5. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,230
    Thanks
    12
    Thanked 116 Times in 114 Posts
    nah, exit can be useful to the PHPcentric mindset ... agree with namespaces and so much of the overhead introduced trying to make PHP==$other_proper_language which it thankfully never will.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  6. #6
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    2,875
    Thanks
    3
    Thanked 411 Times in 400 Posts
    Quote Originally Posted by firepages View Post
    nah, exit can be useful to the PHPcentric mindset
    Thing is what it DOES is useful, but do we really need TWO separate functions -- die and exit -- that do the exact same thing? EXACT same thing, die() is just an alias...

    At which point it's like the mind-numbingly dumbass functional wrappers for mysqli. I thought PHP 7 was supposed to get rid of pointless redundancies to speed up both the parser and bytecode stages -- there are some painfully obvious ones that should take little effort to axe.

    But what do I know? I'm the nutter who wants to see <?php ?>, <?=, heredoc, nowdoc, and double-quoted strings removed from the language. Pick one thing, then optimize the F*** out of it.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  7. #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,230
    Thanks
    12
    Thanked 116 Times in 114 Posts
    as you say, just an alias essentially zero overhead.

    The thing that really irks me is inconsistency in bi-polar argument orders for the array and string functions, no matter how long I have been at this- I still forget from time to time.
    .. the the only option to 'fix' this, is to break lots of code, probably lots of, from one perspective - crappy code , and from another perspective - working code that's been working happily for $years and simply does not need breaking, how much mind-numbing PERL code is still out there driving this that or the other but is never going to be fixed or updated since nobody has a clue how it works, and it works?

    e.******g. , I have a small socket server which sits there waiting for labels to print since my label printer at work never played nicely with linux so it is hooked up to a windows machine which listens and prints...at least it did , it had been working happily on 5.$? for years, then after upgrading the machine for some reason or another and installing the latest cli it breaks..why?

    PHP Code:
    $yaks='';
    $yaks['something']='something else';
    ...
    etc
    Well $yaks now equals true ... PHP Warning: Illegal string offset...
    so ok the fix was $yaks=NULL; ($yaks=array() originally had some other issues that I can not recall)
    ...even more annoying is that this is a warning, not fatal - which at least would have been found instantly.

    And yes from a strongly typed perspective, turning a string into an array was stupid in the first place, but really , in the grand scheme of things it hurt nobody~ until now.

    These days for me everything is templated and php and HTML rarely meet, but when just running some quick test code <?= is still useful and again, hurts nobody, and whilst PHP remains an interpreted language, you are going to need a delimiter.

    From a purists point of view PHP is crap, but that's easily fixed by simply not using it in the first place.

    You cant have everything that made PHP so popular gathering such widespread usage AND please the purists , end of
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  8. #8
    New Coder
    Join Date
    Jun 2018
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Didn't really expect people to go on a rant about PHP, WP etc. in a PHP subforum.

    My only question is, why would I use namespaces if using the pci_ "namespace" will reduce the probability of conflicts with external libraries by 99,9%, now and in the future. This is an app with a single developer (me), which needs to scale well, because the client has all sorts of ideas already. I have full control over the folder structure aswell. Does it make sense to use qualified PHP namespaces in such a case, and if so, why?

  9. #9
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,651
    Thanks
    24
    Thanked 580 Times in 573 Posts
    If you have deep folder structures, your class names get uncomfortably long, e.g. Swift_Transport_Esmtp_Auth_LoginAuthenticator. That might get annoying if you have to write that class/interface name more often, esp. in type hinting.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  10. #10
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,230
    Thanks
    12
    Thanked 116 Times in 114 Posts
    Didn't really expect people to go on a rant about PHP, WP etc. in a PHP subforum.
    we don't need much of an excuse but apologies for hijack
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  11. #11
    Regular Coder Vege's Avatar
    Join Date
    Jan 2008
    Posts
    937
    Thanks
    22
    Thanked 125 Times in 124 Posts
    Benefits come with huge projects and when multiple people are working on one project or developers change allot.

    You can for example change the namespace and not change the classname when refactoring code. Test code can be identical with just changed namespace to test.

  12. #12
    New Coder
    Join Date
    Jun 2018
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    If you have deep folder structures, your class names get uncomfortably long, e.g. Swift_Transport_Esmtp_Auth_LoginAuthenticator. That might get annoying if you have to write that class/interface name more often, esp. in type hinting.
    I don't reflect the folder structure in my class names, instead I pass an array with the folder names to the autoloader function. And the way I see it currently, namespacing will require more typing than just adding a simple pci_ prefix to classes.


    > Benefits come with huge projects and when multiple people are working on one project or developers change allot.

    That's not the case here either. So I suppose I will either implement namespaces just to learn it and get used to it, but w/o much actual benefit, or I'll wait until I get a project where it's worthwhile.

  13. #13
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    2,875
    Thanks
    3
    Thanked 411 Times in 400 Posts
    Quote Originally Posted by Vege View Post
    You can for example change the namespace and not change the classname when refactoring code. Test code can be identical with just changed namespace to test.
    Yeah, that's the part I don't get. If you change the namespace you have to change everywhere that calls the namespace, so what's the huffing difference?!?

    It makes no sense, it's a pointless redundancy which is why I advise people to forget that namespace garbage even exists. It basically comes across as a half-assed half-witted attempt at creating proper library includes without actually providing proper library includes!

    Which is why @paddelboot my advice is exactly that. You seem to be discovering the same issue I have with it; it makes the code pointlessly aggravatingly complicated and creates more work for ZERO legitimate benefit your existing class isn't already likely providing.

    Quote Originally Posted by paddelboot View Post
    or I'll wait until I get a project where it's worthwhile.
    You may find that to be a LONG wait. I've been programming for four decades now, and PHP namespaces are one of the dumbest things I've ever seen in a programming language that seems to have been created to either address a non-issue, or worse is a half-effort at fixing a deep rooted issue. Either way it fails to deliver on its claims.

    Though failing to deliver on claims seems to be SOP for damned near everything these days.
    Last edited by deathshadow; Jun 14th, 2018 at 10:00 PM.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  14. #14
    New Coder
    Join Date
    Jun 2018
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by deathshadow View Post
    Which is why @paddelboot my advice is exactly that. You seem to be discovering the same issue I have with it; it makes the code pointlessly aggravatingly complicated and creates more work for ZERO legitimate benefit your existing class isn't already likely providing.
    I'm not going to dismiss PHP namespaces entirely, because there must be a reason why people think it's useful for something. I guess it depends on the project/situation. But for now, I will see if I can put my energy into things like unit testing, because there at least the benefit is more obvious, even though also somewhat controversial, especially when you look at the additional effort that's necessary for a new project.
    Last edited by paddelboot; Jun 15th, 2018 at 04:12 PM.

  15. #15
    Regular Coder Vege's Avatar
    Join Date
    Jan 2008
    Posts
    937
    Thanks
    22
    Thanked 125 Times in 124 Posts
    Quote Originally Posted by deathshadow View Post
    Yeah, that's the part I don't get. If you change the namespace you have to change everywhere that calls the namespace, so what's the huffing difference?!?
    If all your classes are under one namespace then changing one line per page vs all the classnames on every page is a huge difference.


    But adding a namespace is just a good way to be sure there will be no name conflicts. That's what it's for. That's all it should be for.
    That's why it's used in every language.
    It's also more clear to create User class rather than MyNamespaceUser. Classes should be reusable and not tied to one project.


 
Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

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