wordpress-handbook icon indicating copy to clipboard operation
wordpress-handbook copied to clipboard

Add class organization in PHP standards

Open dingo-d opened this issue 6 years ago • 2 comments

Add example and explanation about visibility ordering of methods and properties in classes like described here: https://stackoverflow.com/a/310967

dingo-d avatar Jan 25 '19 13:01 dingo-d

Proposed organization within a class

class Class_Name {
  const CONSTANT_NAME = '...';

  public $public_property;
  protected $protected_property;
  private $private_property;

  public function __construct() {...}

  public function public_method() {...};

  public static function public_static_method() {...};
  protected static function protected_static_method() {...};
  private static function private_static_method() {...};

  public abstract function public_abstract_method();
  protected abstract function protected_abstract_method();
  private abstract function private_abstract_method();

  protected function protected_method() {...};

  private function private_method() {...};
}

Constants first, then properties (avoid using public properties), after that a class constructor and destructor (rarely used in WP context), public methods, static (avoid if possible) and abstract methods, then protected and private methods (internals).

dingo-d avatar Jan 28 '19 09:01 dingo-d

Could go here: https://infinum.com/handbook/books/wordpress/coding-standards/php-coding-standards/naming#class-method-visibility

dingo-d avatar Apr 16 '21 15:04 dingo-d