views:

58

answers:

4

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 names 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?

A: 

It depends not only on how many classes you're going to have, but also how many sites and apps you will have. If you have more than one site or app, it would be more logical to have folders so you don't get confused. If you only have a few classes (say less than 20), it might be more logical just to keep them all in one folder. Based on the kinds of classes above and how detailed you want to be, I'd go ahead and use directories so that later on you don't look at it and say "Gosh I wish I had used directories". Then you end up writing useless programs that you'll only ever use that one time just so you can change your file structure.

animuson
A: 

I would suggest Option 1, because in that layout in particular is the separation of codes, which will end up as a much manageable and flexible system.

Starx
A: 

You could follow Zend Framework standards like

Option1 with autoload

new App_Encryption_Cipher

in autoload magic callback replace the _ to '/' and do further checking (the file exists? garbage in the filename being seek? symbols?)

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.

Depends on how you plan/design your application. there is no escape when it comes to refactoring anyway :)

I know you are used to java naming conventions, but if you do something like (new com_mysite_myapp_encryption_cypher) well, it kinda becomes a pain to write all the time

allenskd
+1  A: 

I've always preferred prefixed files with fewer folders so there is less navigating around, but it is just my personal preference.

if you think you might want to change it later, you can create a central include script like:

<?php do_include('encryption_cypher'); ?>


   function do_include($file){
    if($file== 'encryption_cypher')
        include('class/app/someotherfolder/encryption/cypher.php');

    }

However, this is just messy in the long-term, so pick the lesser of the two evils and go.

Aaron Harun