fluffigt.com *poof* said fluff….

23Aug/080

Private methods in PHP programming – response

Felix over at Debuggable wrote an interesting article about private methods. He thinks it's not a very good thing to use, but, kudos to him, doesn't rule out the possibility. He argues that he doesn't have any formal education in programming and development, but I don't think that matters much at all since he seems to have quite a bit of programming experience.

But what I think is that, of course, you alwas can write private-method-free code, but sometimes it's not very good. When you need to duplicate code time after another, you might begin to see the need for another method (refactoring) or for that matter class. What that method does or returns maybe, just maybe, isn't that good for the rest of the world (read application) to access directly rather than through your other, more high level methods.

CakePHP itself is has lots of private methods. Could we do without them all? Of course we could. Would it make the code easier to read? Maybe. Would it make the code easier to maintain? I think not.

With that said, you always need to make an active decission whether you choose to make a method public, private or protected. Chances are, if you only write public because you are used to it, you might be making a mistake that could compromise stability or security of your application if another developer makes use of that method...