Article

Home » Server-side Coding » PHP & MySQL Tutorials » What's new in PHP 5.3?
SitePoint Feature Article

About the Author

Ben Balbo

Ben Balbo Ben Balbo was born in Germany, grew up in the UK, lives in Melbourne, and brews his own beer. A software developer and open source community liaison by day, Ben is the Vice-President of the Linux Users of Victoria, Treasurer of the Open Source Developers' Club and convener of the Melbourne PHP Users Group and BarCampMelbourne. He frequently speaks at Australian and international conferences and events on a broad range of topics. He also drinks a lot of coffee.

View all articles by Ben Balbo...

What's new in PHP 5.3?

By Ben Balbo

February 13th, 2008

Reader Rating: 8

Page: 1 2 Next

PHP 6 is just around the corner, but for developers who just can't wait, there's good news -- many of the features originally planned for PHP 6 have been back-ported to PHP 5.3, a final stable release of which is due in the first half of this year.

This news might also be welcomed by those that wish to use some of the new features, but whose hosting providers will not be upgrading to version 6 for some time -- hosting providers have traditionally delayed major version updates while acceptance testing is performed (read: the stability has been proven elsewhere first). Many hosting companies will probably delay upgrading their service offerings until version 6.1 to be released. A minor upgrade from 5.2.x to 5.3, however, will be less of a hurdle for most hosting companies.

This article introduces the new features, gives examples of where they might be useful, and provides demo code to get you up and running with the minimum of fuss. It doesn't cover topics such as installing PHP 5.3 -- the latest development release of which is currently available. If you'd like to play along with the code in this article, you should install PHP 5.3, then download the code archive. An article on installing PHP 5.3 can be
found on the Melbourne PHP Users Group web site.

Namespaces

Before the days of object oriented PHP, many application developers made use of verbose function names in order to avoid namespace clashes. Wordpress, for example, implements functions such as wp_update_post and wp_create_user. The wp_ prefix denotes that the function pertains to the Wordpress application, and reduces the chance of it clashing with any existing functions.

In an object oriented world, namespace clashes are less likely. Consider the following example code snippet, which is based on a fictional blogging application:

<?php
class User {
 public function set( $attribute, $value ) { ... }
 public function save() { ... }
}
$user = new User();
$user->set('fullname', 'Ben Balbo');
$user->save();

In this example, the save method will not clash with any other method, as it is contained within the User class. There's still a potential issue though: the User class might already be defined by some other part of the system if, for example, the blogging application runs within a content management system.
The solution to this issue is to use the new namespaces keyword. Taking the above code again, consider the following sample files:

<?php
namespace MyCompany::Blog;
               
class User {
       
       public function set( $attribute, $value ) {
               $this->$attribute = $value;
       }
       
       public function save() {
               echo '<p>Blog user ' . $this->fullname . ' saved</p>';
       }

}

<?php
$user = new MyCompany::Blog::User();
$user->set('fullname', 'Ben Balbo');
$user->save();

On the surface, the advantages offered by namespacing our function might not be immediately obvious -- after all, we've simply changed MyCompany_Blog_User to MyCompany::Blog::User. However, we can now create a User class for the CMS using a different namespace:

<?php
namespace MyCompany::CMS;

class User {

       public function set( $attribute, $value ) {
               $this->$attribute = $value;
       }
       
       public function save() {
               echo '<p>CMS user ' . $this->fullname . ' saved</p>';
       }

}

We can now use the classes MyCompany::Blog::User and MyCompany::CMS::User.

The use Keyword

Addressing classes using the full namespace still results in lengthy calls, and if you're using lots of classes from the MyCompany::Blog namespace, you might not want to retype the whole path to the class every time. This is where the use keyword comes in handy. Your application will most likely use a number of different classes at any given time. Say, for example, the user creates a new post:

<?php
use MyCompany::Blog;
$user = new Blog::User();
$post = new Blog::Post();
$post->setUser( $user );
$post->setTitle( $title );
$post->setBody( $body );
$post->save();

The use keyword is not restricted to defining namespaces in which to work. You can also use it to import single classes to your file, like so:

<?php
use MyCompany::Blog::User;
$user = new User();

Namespace Aliases

Earlier, I pointed out that one advantage of namespacing is the ability to define more than one class with the same name in different namespaces. There will obviously be instances where those two classes are utilized by the same script. We could just import the namespaces, however, we also have the option of importing just the classes. To do so, we can use namespace aliasing to identify each class, like so:

<?php
use MyCompany::Blog::User as BlogUser;
use MyCompany::CMS::User as CMSUser;

$bloguser = new BlogUser();
$bloguser->set('fullname', 'John Doe');
$bloguser->save();

$cmsuser = new CMSUser();
$cmsuser->set('fullname', 'John Doe');
$cmsuser->save();

Class Constants

Constants are now able to be defined at the class level! Note that class constants are available when you're importing namespaces, but you cannot import the constant itself. Here's an example of how we might use them:

<?php
namespace MyCompany;

class Blog {
       const VERSION = '1.0.0';
}

<?php
echo '<p>Blog bersion ' . MyCompany::Blog::VERSION . '</p>';

use MyCompany::Blog;
echo '<p>Blog version ' . Blog::VERSION . '</p>';
       
use MyCompany::Blog::VERSION as Foo;
echo '<p>Blog version ' . Foo . '</p>';  
This will result in the following output:
Blog bersion 1.0.0
Blog version 1.0.0
Blog version Foo

Namespaced Functions

The use of static class methods has deprecated the use of functions in the object oriented world in which we now live. However, if you do need to add a function to your package, it too will be subject to namespacing!

Here's an example:

<?php
namespace bundle;
function foo() { echo '<p>This is the bundled foo</p>'; }
foo(); // This prints 'This is the bundled foo'

<?php
function foo() { echo '<p>This is the global foo</p>'; }
require( 'lib/bundle.class.php');
bundle::foo(); // This prints 'This is the bundled foo'
foo(); // This prints 'This is the global foo'

The Global Namespace

The global namespace is an important consideration when you're dealing with functions. In the previous example, you'll notice that there's no direct way of calling the global foo function from within the bundle code.

The default method of resolving calls to functions is to use the current namespace. If the function cannot be found, it will look for an internal function by that name. It will not look in other namespaces automatically.

To call the global foo function from within the bundle namespace, we need to target the global namespace directly. We do this by using a double colon:

<?php
namespace bundle;
function foo() { echo '<p>This is the bundled foo</p>'; }
foo(); // This prints 'This is the bundled foo'
::foo(); // This prints 'This is the global foo'

If you liked this article, share the love:
Print-Friendly Version Suggest an Article