Order of class methods?
Moderator: General Moderators
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Order of class methods?
Sup pals
Out of curiosity, how do you order the methods of your classes? Do you just leave them in the order you wrote them, or do you order them alphabetically or based on the purpose of the methods?
Out of curiosity, how do you order the methods of your classes? Do you just leave them in the order you wrote them, or do you order them alphabetically or based on the purpose of the methods?
Re: Order of class methods?
I order by function, and usually in the order that I use them in my page. Functions that are used first are listed first in the class, and then related functions. If there is a function used in another function, though, I put that above the function that it's used in.
Complicated?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Order of class methods?
I just looked through the code of a couple projects and got this general order:
1. constructor
2. initialization
3. setters and getters
4. other public methods but there are setters mixed in
5. private / utility methods.
6. sometimes I see magic methods at the very bottom
Not sure why the order matters that much, other than properties and then the constructor at the top -- which is very standard.
1. constructor
2. initialization
3. setters and getters
4. other public methods but there are setters mixed in
5. private / utility methods.
6. sometimes I see magic methods at the very bottom
Not sure why the order matters that much, other than properties and then the constructor at the top -- which is very standard.
(#10850)
Re: Order of class methods?
Forgot about the constructor, and the technical names. Since I usually initialize first, and since the magic stuff doesn't really fit in anywhere else, I tend to put them in the same places Arborint stated. Nice to know I'm not totally crazy.1. constructor
2. initialization
3. setters and getters
4. other public methods but there are setters mixed in
5. private / utility methods.
6. sometimes I see magic methods at the very bottom
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Order of class methods?
Interesting. I have my constructors in the top, too, but some magics like toString are at the bottom for some reason... getters and setters are at the top, too. The rest of the methods, publics are above protected/private ones, but other than that, there is no rule of how do I order them.

Well, it doesn't really matter to me, but it always makes me muse, because there is no clear logic other than the basics - e.g. if you have two public methods, they would be in random order which is illogical.arborint wrote:Not sure why the order matters that much
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: Order of class methods?
I would say it does matter, in the sense that convention and consistency only serve to speed up your development efforts.Well, it doesn't really matter to me, but it always makes me muse, because there is no clear logic other than the basics - e.g. if you have two public methods, they would be in random order which is illogical
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
Re: Order of class methods?
Maybe. I tried to look at Zend's developer zone for information, but came up with nothing. They want you to use null over NULL, but they don't care if you put __toString() in the top of the class while public attributes are in the end of the class (and having this order changing randomly between files)... which I find a bit odd. Perhaps I have missed something.
Re: Order of class methods?
Constructor, loaders, savers, magic.
Thinking about it, I think my methods are ordered with the methods that are most likely the reason the user is looking at the code, to the least likely. For example, if I've got a simple __get() that just returns private object variables, I'll put that at the bottom. If I do a bunch of fancy logic, and make __get() return values for variables that aren't object variables, I'll put that higher up. Similarly, I put my load() functions higher than, say, generateRandomString().
Thinking about it, I think my methods are ordered with the methods that are most likely the reason the user is looking at the code, to the least likely. For example, if I've got a simple __get() that just returns private object variables, I'll put that at the bottom. If I do a bunch of fancy logic, and make __get() return values for variables that aren't object variables, I'll put that higher up. Similarly, I put my load() functions higher than, say, generateRandomString().
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
-
BornForCode
- Forum Contributor
- Posts: 147
- Joined: Mon Feb 11, 2008 1:56 am
Re: Order of class methods?
1. initialisation
2. constructor
3. public methods
4. getter/setter area
5. private/protected area
Usually when i open a class i'm interested by its public methods (in code analysis, debugging etc), that's why i put private and protected at the end (since many IDE's have the beautiful "Go to declaration" navigating to them is easy).
2. constructor
3. public methods
4. getter/setter area
5. private/protected area
Usually when i open a class i'm interested by its public methods (in code analysis, debugging etc), that's why i put private and protected at the end (since many IDE's have the beautiful "Go to declaration" navigating to them is easy).