tags:

views:

46

answers:

2
+1  Q: 

packages in php?

in java you can have same type of classes and interfaces in same package.

i wonder how you could organize code in this way in php.

have them in different folders? or use extend on them so you at least can organize them by superclass? (which you could do in java also).

+2  A: 

Recent versions of PHP have namespaces, but other than those it's a complete free-for-all.

Ignacio Vazquez-Abrams
Nested classes? That's not true. PHP never had nested classes.
Ionuț G. Stan
Hrm. I thought it had. Will remove.
Ignacio Vazquez-Abrams
Well... strictly speaking, you can have functions or classes nested inside functions, but they'll be defined in the global scope.
Ionuț G. Stan
+2  A: 

With PHP < 5.3, there is no "real" notion of "package" ; what's generally done is name classes like this : Package_Subpackage_MyClassName, and map it to this structure for files and directories :

Package/
    Subpackage/
        MyClassName.php

This corresponds to the PEAR Convention, which is widely used, and well accepted :

Class names should always begin with an uppercase letter. The PEAR class hierarchy is also reflected in the class name, each level of the hierarchy separated with a single underscore.

As long as you follow that convention, you can do pretty much whatever you want -- a good idea might be to take a look at the sources of some Open-Source Framework that follow that convention, such as Zend Framework or Doctrine.


With PHP >= 5.3, you have namespaces -- which means things might/will change a bit... But, as it's still quite new, the "best pratices" are not yet well defined nor accepted.

The PSR-0 Final Proposal might be an interesting read, though...

Pascal MARTIN
Glad you brought up namespaces, definitely a major improvement in PHP 5.3.
Jay Zeng