简体   繁体   English

你如何组织一个类中的方法和属性?

[英]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 如果我有时间,我会尝试按字母顺序排列; 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) 然后我通常把构造函数作为第一个方法(或者如果它是一个带有“init”的类或者一个框架调用的其他方法,我会把它放在顶部)

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. 拥有像Eclipse + PDT或vsPHP这样的IDE会向您显示类的大纲视图,您可以根据需要对方法进行排序,这样您就不必去寻找代码了。

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). 它是大多数经常编辑到最不经常编辑的事物的排序组合,并且使重要方法中显而易见的事情变得明显(这就是为什么魔术方法比通常更高的原因)。

我想我在一个函数中所做的唯一一种组织是将__construct放在前面,然后在类上增长而没有任何组织,但我通常从非公共函数开始并完成公共函数

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. 对于这些方法,我首先选择getter和setter,然后将其他内部方法松散地组合在一起,然后使用__construct和其他魔术方法,任何静态方法都可以使用....但我很少会完全保持这种顺序。

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. 使用Visual Studio和Resharper意味着它在很大程度上是不必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你如何在PHP中组织你的页面? - How do you organize your page in php? 如何组织PHP中的类并从类中访问数据库 - How do I organize classes in PHP and access database from within class 您是否认为PHP中的错误形式是在类方法中访问超级全局变量? - Do you consider it bad form in PHP to access super globals within class methods? 如何使用SLIM Microframework组织大型Rest API? - How do you organize large Rest APIs with SLIM Microframework? 如何在CodeIgniter中组织模板结构? - How do you organize your template structure in CodeIgniter? 如何组织从目录回显的文件? - How do you organize files that are being echo'd from a directory? 如何在单个超映射类中创建 ManyToOne 关系 - How do you create ManyToOne relation within single supermapped class 如何在Laravel中注册类似于Application.php中的自定义方法 - How do you register custom methods like those within Application.php in Laravel 如何从PHP的父类中列出类的子方法而没有静态信息? - How do you list child methods of a class from a parent class in PHP without statics? 如何迭代当前的类属性(不是从父类或抽象类继承)? - How do you iterate through current class properties (not inherited from a parent or abstract class)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM