Say you're declaring a class with all the bells and whistles - constructor and destructor, public, private, protected and static methods and properties, magic methods, etc.

How do you organize all this logically? For instance, do you group things by visibility? Do you alphabetize method names? Do you group magic methods together? Do you put the constructor at the beginning and the destructor at the end?

Obviously this is subjective, but I'm curious to hear what has worked for others, or what you find easy to navigate when reading others' code.

+8  A: 

First fields by visibility

Constructor and destructor

Then methods by visibility (public, protected, private) - magic first, then instance, static last

If I have time, I try to put them in alphabetic order ;P

I rarely have static methods so I omitted those. Will update the list
@Mchl: By visibility = most to least? Like public, protected, private?
Nathan Long
@Mchl: Magic methods before others?
Nathan Long
Updated to answer the comments ;)

Personally, I put class variables at the top (by visibility), then magic methods, then public methods, then protected / private methods. It's a combination of ordering things in most-often-edited to least-often-edited and making it obvious what's going on in the important methods (which is why the magic methods are higher than they normally would be).


I guess the only kind of organisation I do within a function is putting __construct to the front from then on the class grows without any kind of organisation on my part but i usually start with nonpublic functions and finish with the public functions

Christian Smorra

Personally, I have class constants at the top; properties next, trying to maintain in order of private, protected, then public. For the methods, I go for getters and setters first, then other internal methods loosely grouped together, followed by __construct and other magic methods, with any static methods last.... but I rarely end up keeping absolutely to that ordering.

Mark Baker
+2  A: 

like this

class Foobar 
      var $public;

      function __construct(....

      function public_method_1()...
      function public_method_2()...


      var $_priv;

      function _private_1()...
      function _private_2()...

basically, most interesting (for class users) stuff first

I like the "most interesting first" idea
Nathan Long

To be honest (and this is going to sound like bad practice) I don't make any effort to arrange them in any particular order. Working with Visual Studio and Resharper means its largely unnecessary.

I don't know those programs - why do they make order irrelevant? I'm using Zend Studio, and it will show me an outline of methods if I want. The outline is easier to scan if it's in some order. What's different in your setup?
Nathan Long
+1  A: 

I put static vars first, class variable next. then i generally put the constructor as the first method (or if it is a class with "init" or some other method called by a framework I'll put that at the top)

After that I try to just keep related methods grouped together so as to have the least amount of scrolling, but it can get messy after a while.

Having an IDE like Eclipse + PDT or vsPHP will show you the outline view of your class and you can sort the methods as you like so you don't have to go hunting through the code.