How do you organize methods and properties within a class?

Viewed 5796

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 Answers

I know it's an old and forgotten post, but I've come across ordering my class members today and here are my five cents:

abstract class Stdclass
{
   public const CONSTANT;
   protected const CONSTANT;
   private const CONSTANT;

   use TraitA, TraitB;

   public static $property;
   protected static $property;
   private static $property;

   abstract public static function method(); 
   abstract protected static function method(); 

   public static function method() {} 
   protected static function method() {} 
   private static function method() {}

   final public static function method() {} 
   final protected static function method() {} 

   public $property;
   protected $property;
   private $property;

   abstract public function method(); 
   abstract protected function method();

   public function __construct() {} 
   public function __destruct() {}
   public function __otherMagicMethods() {}

   public function getterMethod() {}
   public function setterMethod() {}
   public function method() {}
   final public function method() {}

   protected function method() {}
   final protected function method() {}

   private function method() {}
}
Related