Class structure

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Class structure

Post by GeXus »

How do you guys setup your class structure? As in, how do you typically break it up? Do you have one class per file or one big file with all classes...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Class per file unless they are skeleton/minimalist that are highly related classes.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

One class per file is the easiest to maintain and organise - you can use any number of tools afterwards to aggregate source code in one giant file to maximise performance if needed.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post by GeXus »

Do you separate classes based on area of the site? for example, I have classes that I use primarily for the admin area, however some of the methods I would still use outside the admin.... would you create new ones or keep them all in one class dir and just use them where ever?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Directory structure wise? I segregate my classes based on categories. We've had a few discussions on that, so instead of rehashing my response, I'll simply link to one of them:

viewtopic.php?t=46498
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Post by EricS »

I separate each class into it's own file. I also stick to a standard naming convention for those classes and files which allows me to autoload the classes I need in the most efficient manner possible. I also wrote my own autoloader which is optimized for looking in the most commonly used folders first.

As for segregation. I really liked the directory structure used by Rails so I more or less duplicated it.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Class structure

Post by thinsoldier »

GeXus wrote:How do you guys setup your class structure? As in, how do you typically break it up? Do you have one class per file or one big file with all classes...

I have 1 file for any class that's a lot of code or extends some built in php class.
For something like my form field building class where theres FormField and then an extended class for each type of field, since they're all so related I have them in a single file.

For use with autoload() I have to make special cases in my autoload function for files that contain multiple classes. Not a big deal.
Post Reply