Search code examples
phpnamespacesdirectory-structure

Using Java-Style Namespace Convention in PHP or is it better to use X?


I know that Namespaces in PHP are relatively new and so there are many possibilities to use them. Because it was not usable on the Server i did define my classnames like folder_folder_classname if they where in /folder/folder/classname.php

Now i want to change that to get better class names. I thought to redesign my class structure to a java based one, like com\domain\project\folder\folder\class (e.g. in java: com.domain.project.folder.folder package structure) and was thinking that this is a nice solution to avoid any problems with other classes i ever use and don't think about include() for my classes any more :)

So now my Questions:

  • Is this a good idea or should I use a better system for my php classes?
  • Is there a common handling for that in php? (I saw that Zend Framework uses e.g. instead of Zend_Controller_Request_Abstract the namespace Zend\Controller\Request with class name RequestAbstract -> no domain and double wording...)

thanks for your answers :)


Solution

  • I would definitely avoid using the domain wording and just prepend the project name itself like Zend does (that way you avoid collision, too). Since you are thinking about restructuring and if you are using PHP > 5.3.0 (which you should) you might also want to consider the PHP namespaces.