Pages

Categories

First signs of namespaces in PHP 6

Every so often I grab the latest PHP 6 snapshot from snaps.php.net just to see what’s been going on in the development of it. Today I downloaded a snapshot as usual and was pleased to see they’ve already started to include support for namespacing.

Unicode improvements aside, namespacing is something I’ve longed for in PHP for a long time. It’s not a rocket-science approach they’ve taken; effectively the implementation just prefixes the current namespace onto class and function names which are now permitted to contains the double-colon (::) syntax in their names.

You can achieve an almost Java-like package handling system by using __autoload() (or the SPL alternative) though.

My experimentation thus far is this:

autoload.php

<?php

define("BASE_DIR", dirname(__FILE__));

function __autoload($class) {
  $file = BASE_DIR . "/" .str_replace("::", "/", $class) . ".php";
  if (is_file($file)) {
    require_once $file;
  }
}

Foo/Thing/Example.php

<?php

namespace Foo::Thing;

class Example {
  public function __construct() {
    echo "Foo::Thing::Example instantiated…\n";
  }
}

Foo/Thing/AnotherExample.php

<?php

namespace Foo::Thing;

class AnotherExample extends Example {
  public function __construct() {
    parent::__construct();
    echo "Foo::Thing::AnotherExample instantiated…\n";
  }
}

Bar/Example.php

<?php

namespace Bar;

class Example {
  public function __construct() {
    echo "Bar::Example instantiated…\n";
  }
}

test.php

<?php

require_once "autoload.php";

import Foo::Thing::Example;
import Foo::Thing::AnotherExample;

$a = new Example();

$b = new Bar::Example();

$c = new AnotherExample();

And the output…

d11wtq@pc-cac:~/php6$ php test.php
Foo::Thing::Example instantiated…
Bar::Example instantiated…
Foo::Thing::Example instantiated…
Foo::Thing::AnotherExample instantiated…
d11wtq@pc-cac:~/php6$

It’s certainly a step in the right direction, but (and let’s not be too cruel — it only appeared in the past few weeks!) some quirks I have noticed.

  1. You cannot import both Foo::Thing::Example and Bar::Example inside the same file. You need to use the full name for at least one of them.
  2. No warnings are generated if you try to use something which doesn’t exist (No::Such::Namespace)
  3. It’s not possible to import everything by wildcard (import Foo::Thing::*;) but I can live with that since it usually leads to poor programming

I’m looking forward to seeing how this pans out :)

This entry was posted on Friday, August 17th, 2007 at 2:16 pm and is filed under PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

5 Responses to “First signs of namespaces in PHP 6”

  • Bramus! [ 17Aug07]

    Would be great indeed to see this implemented (preferably in a C#/AS3-kind of way). Good to see where it’s headed. Aah, the future looks good indeed :)

  • Bram.us » PHP6 and namespaces? [ 17Aug07]

    […] this is so geeky, but the first signs of namespaces in PHP6 get me alle excited … Spread the […]

  • Aaron Saray [ 17Aug07]

    Hey - thanks for the informative post. I haven’t been following exactly how namespaces are going to be implemented in php6 yet. Once they fix quirk 1 and 2, I think I’ll take a snap and check it out. If not for those, the namespace thing is almost possibly the same as a homegrown one in PHP5. *crosses fingers*
    -aaron

  • d11wtq [ 10Oct07]

    Some people may not be suprised to hear that this is actually going to be available as of PHP 5.3. Oh yes… wait, do I hear cries of bemusement? :)

  • Adam [ 15Nov07]

    I was actually reading another blog a moment ago who said that PHP finally has namespaces - got me very excited! I just assume that people are seeing the first signs of namespacing and it’s not actually fully implemented yet. After all, we know what happened in PHP5, first it had namespaces and then it was taken away at the last minute. It’s something we most certainly need, however, and these developments look ever increasingly promising.

Leave a Reply







XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>