Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you organize methods and properties within a class?

Tags:

php

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.


2 Answers

  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 image 181
4 revsMchl Avatar answered Sep 12 '25 07:09

4 revsMchl


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

like image 32
user187291 Avatar answered Sep 12 '25 08:09

user187291