NederlandsEnglishBulgarian (Български)
sectionimage
Organize Your Project like a Pro with PHP Namespaces
Tuesday, 20 April 2010 08:09

PHP took a huge step forward in June 2009 with the release of version 5.3. It wasn't quite as big of a change as version 5.0, but the release did introduce a number of new language features including namespaces — an addition that can dramatically improve the way you organize your PHP in an object oriented paradigm.


Anyone who's written code in Java, C++, or for any language in ASP.NET has had the pleasure of working with them, but we PHP developers have had to settle with emulating namespaces by defining long class prefixes.


Prefixes serve their primary purpose well: when used correctly, they will ensure that your class names do not have naming conflicts. Unfortunately, they are long and pedantic, and provide no additional benefits. Take, for example, the following class name: Zend_Service_DeveloperGarden_Response_ConferenceCall_CreateConferenceResponseType which can be found in Zend Framework. At 81 characters, the name of this class by itself breaks the recommended line length in Zend's own coding standard, and it is not even the longest class name in the framework!


Namespaces provide other benefits than shorter class names, though. Consider the following:


<-- code here -->


The namespace declaration immediately gives you a clear indication of what type of class is being declared.


In less than a year, industry-leading frameworks such as Zend Framework and Symfony have decided to utilize these new features to such an extent that 5.3 will be a minimum requirement.

 

Source: http://epixa.com/

< Back