The road to PHP 5.3: Namespaces

  • PHP
  • August 12, 2008

We have all been looking forward to PHP6 and the big changes that were proposed for it, but along the way the PHP Core dudes made a great decision and split the PHP6 release in two. Most of the new features expected for PHP6 will be implemented in a 5.3 release, leaving unicode for the PHP6 release. So let’s take a quick look at what’s coming along in PHP 5.3.

Roadmap

On the last few weeks great steps have been taken, and a cool timeline is now up. The release went into feature freeze on the 24th of july, and into alpha1 last week. After that the next 2-3 weeks will see loads of beta and RC releases and finally from mid September to October we will have the final stable release \o/

Features

On this post I’ll talk about Namespaces, and cover other features in future posts.

namespaces

This is by far one of the most expected features that will be included in this release. Like Java or other languages, this will allow developers to group classes and other stuff in namespaces, like below:

<?php /\*\* classes/my/foo/MyClass.php \*/

namespace my::foo;

class MyClass {}

// You can define functions and constants in the namespace too. function myFunc() { } const MY\_CONST = ‘foo’;

?>

So that way you can use this in many forms, like:

<?php /\*\* test.php \*/ include(’classes/my/foo/MyClass.php’);

// You can always access anything ∈an object with fully qualified name. $foo = new my::foo::MyClass();

// You can use the use statement to import a namespace. use my::foo; // After the above statement you can reffer for the my::foo namespace with foo. $foo = new foo::MyClass();

// You can import only one class. use my::foo::MyClass; $foo = new MyClass;

// You can create aliases. use my::foo as MyFoo; use my::foo::MyClass as MyFooClass; $foo = new MyFoo::MyClass(); $foo = new MyFooClass();

// Note, that the following two statements are equivalent: use my::foo; use my::foo as foo;

// You can access functions and constants in the same way: my::foo::myFunc(); myFoo::myFunc(); my::foo::MY\_CONST; myFoo::MY\_CONST;

?>

You can also use “empty” namespaces, so using just (::), like ::MyClass, will ignore current rules and call a globally independent class.

You can read more details here: http://blog.felho.hu/whats-new-in-php-53-part-1-namespaces.html

Next up… Late Static Binding

comments powered by Disqus

Related Posts

dmsAutoComplete v1.1

dmsAutoComplete v1.1

  • August 9, 2006

Publiquei hoje a atualização do meu script de auto-complete (google suggest) compatível com IE e FireFox e baseado em PHP/AJAX.

Depois de publicar a primeria versão do script e com a ajuda de alguns usuários que fizeram testes e utilizaram o script, encontrei algusn errinhos simples nele. Portanto sentei estes dias para achar algumas soluções e agora estou publicando a versão 1.1. Confira algumas das mudanças a seguir neste post.

Read More
2007, novidades e o futuro

2007, novidades e o futuro

  • February 5, 2007

De volta de férias agora volto a me envolver com trabalhos e missões. Acho que a primeira coisa a ser feita é definir os meu objetivos para 2007, pois o Andre Metzen me pegou, e agora devo cumprir minha parte. Este objetivos de certa forma vão me ajudar a descrever o que planejo para este ano, então vamos lá:

Read More
strtotime() - is it useful?

strtotime() - is it useful?

  • September 15, 2006

Every now and then I get e-mails with questions that ask “How can I add X days to a given date?”, “How can I figure the day that corresponds to next thursday?”, and others along the same line. It scares me when I see replies that include enormous codes that execute innumerous function even including some bizarre leap year determination algorithms, I just can’t understand why all the complication and fuss.

The strtotime() function exists to solve these problems and i plan to introduce you to it and show a few usage examples. Also I’m going to check function performance using a simples benchmark comparison.

Read More