Two new features I really like in PHP 5.3

Namespaces
Finally PHP has support for namespaces, not one day too late! Before the days of object oriented PHP name clashes for functions was usually prevented by adding a prefix to your function names. When object oriented PHP was introduced function name clashes was less likely, but class names still had to be unique, ie you could only have one class named for example “User”. This led to the need of prefixing class names. From PHP 5.3 we no longer have to do this!


Namespaces in PHP introduced two new keywords:

  • namespace – define a namespace
  • use – Use a namespace
  • use … as – Use a namespace but give it an alias name

Late static binding

Lambda functions and closures
Lambda functions, or anonymous functions as they are called in PHP, is really something I have missed in PHP. Lambda functions are very convenient to use when you are dealing with callbacks, for example:


Closures are also a very welcome addition to the language. A closure is not the same thing as an anonymous method, which seem to be a widespread misunderstanding. This is the definition from wikipedia of what a closure is:

“In computer science, a closure is a first-class function with free variables that are bound in the lexical environment. Such a function is said to be “closed over” its free variables. A closure is defined within the scope of its free variables, and the extent of those variables is at least as long as the lifetime of the closure itself.” – Wikipedia

In PHP closures will allow us to define closures like this:


Pretty sweet!

Peter

PHP

Leave a Reply

Your email address will not be published. Required fields are marked *