Opinions sought on the best way to organise classes in PHP

Posted by jax on Stack Overflow See other posts from Stack Overflow or by jax
Published on 2010-06-13T16:20:48Z Indexed on 2010/06/13 16:22 UTC
Read the original article Hit count: 237

Filed under:

I am pretty much set on using the Java package naming convention of

com.website.app.whatever

but am unsure about the best way of doing this in PHP.

Option 1:

Create a directory structure

com/
    mysite/
          myapp/
               encryption/
                         PublicKeyGenerator.class.php
                         Cipher.class.php
               Xml/
                         XmlHelper.class.php
                         XmlResponse.class.php

Now this is how Java does it, it uses the folders to organize the heirarchy. This is however a little clumsy in PHP because there is no native support for it and when you move things around you break all includes.

Option 2

Name classes using a periods for the package, therefore sames are just like in Java but can all be in the same directory making __autoload a breeze.

classes/
        com.mysite.myapp.encription.PublicKeyGenerator.class.php
        com.mysite.myapp.encription.Cipher.class.php
        com.mysite.myapp.xml.XmlHelper.class.php
        com.mysite.myapp.xml.XmlResponse.class.php

The only real disadvantage here is that all the classes are in a single folder which might be confusing for large projects.

Opinions sought, which is the best or are there other even better options?

© Stack Overflow or respective owner

Related posts about php