SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)

    Ordering of methods/properties in classes

    Is there a standard? How does everyone else do it?

    For variables I do this order:

    const
    public static
    private static
    public
    protected
    private

    From top to bottom, constants are visible to the most amount of other code, while private variables are visible to no other code, and the others are ordered accordingly. It makes sense but it's based on nothing other than personal preference.

    For methods, I put all static methods first, followed by the constructor, then other magic methods, then the rest of the class methods which I order by the order they're likely to be used in. E.g. and kinf of "set" function will go before a get, and if there's a function which gets called at the end, e.g. an output() function I'll put it at the end of the class definition. I also put related functions, or functions which call other functions within the class next to one another.

    This is all just personal preference and I was wondering how others do it.

  2. #2
    messing with my mind fristi's Avatar
    Join Date
    Feb 2009
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally I don't use any particular order.
    Just the properties on the top, followed by the __constructor() and then all the other methods.

    since in phpdocumentor you can use links, I don't care that much about the order. Maybe I'm just messy
    To PHP or to Perl, that is the question!
    (Bucket - simpletest) User

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for variables, i put constants first, the rest are either alphabetized or sorted by length of variable name. i often try to line up everything into columns for no reason other than it's easier to read for me. for methods, the constructor(s) is first. static methods are last, sometimes spaced down a few extra lines to give it some distance from the object code, because to me they're not a part of the object like the rest of the methods are. also, sometimes i'll try to put public methods before private methods, grouping them together, but this doesn't always work because i like to put private helper methods right next to the method that uses it.

  4. #4
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    __constructor() first
    rest more or less alphabetical (except the setX and getX, those stick together)
    That way, it's easy to find everything.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •