开发者

How do you organize methods and properties within a class?

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.


  1. Constants
  2. Fields by visibility (public, protected, private)
  3. Constructor and destructor and other magic methods
  4. Methods by visibility (public, protected, private)

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


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 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.


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() {}
}


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜