I think PHP programmers across the world are now rejoicing over the recent addition of namespaces into the PHP 5.3 alpha build. Though it isn’t likely we will see a stable release till the middle of September or October, this new feature will give programmers more flexibility in developing programs as well as improve existing frameworks.

Extend core PHP functionality

Previous to the inclusion of namespaces, there was no ability to create custom functions that had the same name as existing functions in the core of the PHP distributions. The concept known as “Global space” will allow a developer to override core functionality which can prove to be very useful.

namespace File;
 
function file_put_contents($file,$data) {
	if( strpos( $file,'ftp://') ){
		// do ftp operation
	}else{
		// will access the core PHP function
		$f = ::file_put_contents($file,data);
		return $f;
	}
}

In that example, I can extend the functionality of file_put_contents to also provide FTP uploading if the path string contains the text “ftp://”. When used within the same namespace, just calling file_put_contents will give you the updated functionality. However, when used in a different namespace, you will have to access it a bit different.

use File;

File::file_put_contents('ftp://localhost/dofile.txt','my content');

Since my transition from PHP to Java, I have started to like the way packages work and would like to see the ability to “use” namespaces and not have to reference the namespace when calling a function from a different namespace.

Overall, this release has brought a lot of needed features (all of which you can probably read up on elsewhere or just hit up the PHP docs) and I am glad to see PHP is moving in the right direction. Looking back, I had a tendency to want PHP to include features found in other languages however I think doing so would alter the direction that PHP has tried to take and perhaps its implementation is the best move to make for both backwards compatibility and expandability while keeping its rapid-development goal.

Share and Enjoy:
  • Digg
  • E-mail this story to a friend!
  • Reddit
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati
  • LinkedIn
  • Slashdot