PHP 5.3 namespaces (before it comes out)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moltendorf
    New Member
    • Jul 2007
    • 65

    PHP 5.3 namespaces (before it comes out)

    I've been looking over some of the newer features of PHP 5.3 and notice that they have updated the namespace documentation. I have been programming with the dev builds with this new system, and have been using the old method of the double colon (::) to refer to certain namespaces. I'm a little confused as to how they reworked it, and am wondering if anyone could explain to me how this new way is handled.

    So I'm just wondering if this is correct... The old namespace system went from...

    File 1:
    Code:
    <?php
    namespace test_module;
    
    class main
    {
    // Code...
    }
    ?>
    File 2:
    Code:
    <?php
    include ('file1.php');
    $name = new test_module :: main ();
    ?>
    To...
    File 2:
    Code:
    <?php
    include ('file1.php');
    $name = new test_modulemain ():
    ?>
    And to reference to sub namespaces, instead of using the double colon, I should just join them together; so for instance, instead of
    Code:
    namespace :: subnamespace
    I use
    Code:
    namespacesubnamespace
    Because, in a sense this completely messes with what I think of as clean code.
    Unless, I can completely ignore the new system, and use the double colon?

    They also mentioned something about backslashes to seperate parent namespace from subnamespaces.

    So could I use for example
    File 2:
    Code:
    <?php
    include ('file1.php');
    new test_module\main ();
    ?>
    I know this post is a little crude and confusing, but please bear in mind that the new documentation completely baffled me (which is unusual since the little symbols like the double colon, or backslash (which they mentioned but did not use in any real code examples) help drastically in understanding what is what).
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Hm. That doesn't look very intuitive.

    I wonder if they've ported these changes to the PHP 6 builds yet. I'll have to play around a bit and see what's going on.

    Comment

    • moltendorf
      New Member
      • Jul 2007
      • 65

      #3
      I'm using the PHP 6 builds as well as that's my intended release platform for my current content management system program I'm building.

      Right now I've got all my variables held in a single class named main in the global namespace (I plan to put it in its own namespace) so in modules that I keep in their separate namespaces (to prevent conflicts as the modules you can have are unlimited) I can reference to any class I have by using things like ::main :: $session -> switch_user ($user_identifi er);

      Or to initiate a module in a looping function I have I'd be like...

      (extensions is a class)
      $namespace = include ($path . 'main' . extensions :: $module);
      Then...
      new $namespace (); (which would be something like test_module :: main);

      I'm not using huge subsubsubsubsub sub namespace setups, but I'm using them nonetheless and whatever happened between then and now is completely baffling. :)

      Comment

      • moltendorf
        New Member
        • Jul 2007
        • 65

        #4
        Alright, it looks like (I guess) their original documentation they uploaded must have had some kind of bug, because now the entire documentation uses a \ for separators of namespaces, whereas it used to not. It makes a whole lot more sense now. :)

        So instead of:
        Code:
        ::main :: $configuration -> get ('some_value');
        $module = new ::module :: session :: login :: main ();
        $module -> render_login_box ();
        I'll just do:
        Code:
        \main :: $configuration -> get ('some_value); // Main is a class, not a namespace.
        $module = new \module\session\login\main ();
        $module -> render_login_box ();
        Phew, I thought I was doomed to using a system in which names weren't even seperated by an ugly underscore. (I thought the Paamayim Nekudotayim was a little more fancy, but backslash is good enough) :P

        Comment

        Working...