My privates are not public, they are protected
This week there was an interesting discussion on twitter between several people from the PHP community on the use of access modifiers, and why things should be public, protected or private, or why not. The thing that triggered this was the fact that the new Symfony2 Coding Style disallows the usage of private methods. This discussion earlier on triggered Lukas Smith to post his opinion. I commented there but the comment became thus long that I decided to write a blogpost about it myself.
In general, I agree with Lukas. I dislike the way private methods block any possible extension. I much more prefer being able to extend something if a piece of code does not support the use case I have. I agree with pro-private people that it is important to have a good API design and to use that to protect less experienced developers from making mistakes, however one should never assume that the developers using your libraries, especially Open Source libraries, are less than yourself. You as developer of a library (especially Open Source), will never think of every use case possible. People may use your code in ways you have never thought of, and they may still be using it in a very valid use case. So they may also run into problems if you make certain functionality private: this will block them from extending your library and in some situations that may block them from actually using your library in valid use cases!
I definitely am not in favor of simply opening up the complete library to every developer though. By making a clear decision on which methods are public and which methods are protected you will ensure that people simply implementing your library will use the API that you have taken the time designing. And in most cases, this will be enough for those developers, and they will be happy users of your code. However, those that wish to extend your functionality are still able to do so. And yes, this will also open up your library to completely invalid use cases for extending, but this is simply not your problem. You have taken the time and put in the effort to design the API to work in the most common use cases and you have kept the option of extending it for other valid use cases, but there will always be developers that abuse the power you've given them.
I see places though where private (and also final) are valid: When you're working on a specific implementation for a customer, you may want to use private to hide specific functionality for that project, or final to block people from overriding the method even if they may call it from subclasses. In this situation, it actually makes a lot of sense, because you will often have architects or seniors building the basis of a project, and have mediors and juniors finishing the project. And in this case, it *is* the responsibility of the senior/architect when power offered by code is abused by less experienced developers. So unless the senior or architect conciously makes the decision to open up this power, it is smarter to not offer it to the implementing developers.
I want to close down with the only reason I can see at this point where private would make sense in Open Source projects. And this is not really from a technical point of view, but more from the community side of things: When you make methods private or final, people who have an alternate use case will have to contribute their patch to the project if (s)he wants it to be changed in the main library (which in most situations would be preferable for those developers I guess). But I don't think the community point of view should rule above the technical reasons for not using private.
July 18, 2010 - tags: php, symfony, Symfony2, access modifiers, protected, private, public
Comments
Ivo: You say "And in this case, it *is* the responsibility of the senior/architect when power offered by code is abused by less experienced developers." but you rule out private. Private allows me to make a method unoverridable if that is safer for the novice. I see no reason how you can apply this logic to protected but not to private.
Seva Lapsha: I disagree with you regarding the senior/medior/junior arguments.
1. Method access level is to manage components relations, not developers. Even if an other senior is willing to alter it, they should respect the initial design and if a change needed, to be fully aware about the reasons why the method is private/protected. If a change is thought to be needed, it should be communicated and discussed.
2. It's the most simple task for the junior one to just go and crack the private method, converting it to public and then using unrestricted. So again, the problem is not the code. The problem is the culture of collaborative work.

martijn: Agree, Seva and Ivo. it aint the code which sucks but the rage against time and cost which kills the code
win a free vacation: Thanks for sharing.
Regard
win a free vacation
insulation orlando: I think being able to use this is so good. This is what makes php so good to use. I just love to be able to use this option.
Impaired driving: You have to do a great job with the security here. There is so much that is important to protect. This is where you have start with. The ideas are so good.
same day payday loan: You have to very careful when it comes to security. You have to be very careful with it these days. There is so much data that needs to be protected and is very valuable.
Beachplace Condos on Longboat Key: There is no cosmetic for beauty like happiness.
fpshmag: You have to very careful when it comes to security. You have to be very careful with it these days. There is so much data that needs to be protected and is very valuable.
ugg bailey button triplet: steelers jerseys for kids suddenly saw their own boxing in many more they do not know the moves, even though they know is wrong, and I can not imagine what will happen then? canada goose parka many people have been regarded as a classic, and admire have added exercise, learn this trick I do not know how many strokes and thus saved their lives! ugg classic mini boots But after a few years later, Shaolin boxing had a lie no longer afraid of what fell to the ground of. discount uggs boots cheap
Sobha Upcoming project: Very good article! This is a highly beneficial blogging site which you have.
Sobha international city villas in gurgaon: A good name is more desirable than great riches; to be esteemed is better than silver or gold.
Discount UGGs : Uggs classic short sale Keep her for a year in the data set. tf42srz
sale uggs boots Oh, the sacrifice of his colleagues million times older than she.
uggs boots sale Yarn under a pillow fragrant bag, filled with rose petals inside.
www.discountuggssale.us He was very honest, "If I do not put the homework first, parents will call me beautiful, but also too important to greet the girls."
toilet problems: I think being able to use this is so good. This is what makes php so good to use. I just love to be able to use this option.
discount uggs: I am thanking you very much for this updation, you have good knowledge about how to write blogs, I appreciate your skill. discount ugg boots
cheap ugg boots: Great blogs I am Appreciating it very much! Looking Forward to Another Great article. uggs boots discount
ugg boots cheap: Good luck to the Author! all the best. discount uggs boots cheap
ugg boots cheap: Good luck to the Author! all the best. discount uggs boots cheap
steelers jersey: Really appreciate this wonderful post that you have provided for us. Great site and a great topic as well i really get amazed to read this. ugg boots cheap
Link Wheel Services: I thank you for taking your time sharing your thoughts and ideas to a lot of readers out there.
seratonin syndrom: You have to very careful when it comes to security. You have to be very careful with it these days. There is so much data that needs to be protected and is very valuable.
tool steel:
Hello, I like you page. I’m glad Yahoo pointed me to it. I was able to get the know-how I was searching so badly for days now.Thank You very much for your really good web page. Have a good day.
used nissan altima: I enjoyed reading your easy transformations. I see you offer priceless info. Stumbled into this blog by chance but I’m sure glad I clicked on that link.
1st aid CPR for child: Great article writing style.Excellent post. I like it. I have decided to share it to other fellows.
LED light bulbs: fantastic article
College Essay Writing Help: Sure now we get a more powerful list but scroll by several pages is not good. Maybe I didn't know how to setup the number of plugins per page. Is there a way to show a lot of plugins per page?
Hispanic Grants: Really appreciate this wonderful post that you have provided for us. Great site and a great topic as well i really get amazed to read this.
uggs australia outlet: Your article is very good, read your article, let me benefit a lot, I think I'll certainly want great read your article.
pellets machine: I have been reading blogs on this subject for a few hours now and your content is the most informed I have read thus far. Thanks for putting this information in one place.
canada goose parkas: you’ve a fantastic blog here!
oakley jawbone: I like your articles, hoped later to see more of such a good article
Buy Power Tools : You can move down "Military history is often considered to be the history of all conflicts, not just the history of the state militaries."
Makita Tool Parts : I cant forget this "The website on laminate flooring offers a great quantity of parquet flooring with competitive price by cutting the middleman and offering products directly to the terminal consumers."
Buy thesis: Great blogs I am Appreciating it very much! Looking Forward to Another Great article. uggs boots discount
how to get big muscles fast: The general strategy adopted by most present-day competitive bodybuilders is to make muscle gains for most of the year (known as the "off-season") and Natural Bodybuilding & huge forum
allopurinol 300 mg: I’m glad Yahoo pointed me to it. I was able to get the know-how I was searching so badly for days now.Thank You very much for your really good web page. Have a good day.
Thesis writing: I was looking at my network connection settings and discovered an option to make my network connection either Public or Private. I am not connected to any other computer at home, nor do I foresee any need to do so in the near future. My sole purpose of my Network connection is to gain access to the internet and retrieve my email. What are the pros and cons to either
setting.
GCSE Coursework: You have to very careful when it comes to security. You have to be very careful with it these days. There is so much data that needs to be protected and is very valuable.
Thesis proposal: the responsibility of the senior/architect when power offered by code is abused by less experienced developers." but you rule out private. Private allows me to make a method unoverridable if that is safer for the novice. I see no reason how you can apply this logic to protected but not to private.
craftmatic prices : This is often significant. Persons stare upon involving written copy when we are baffled. We're fascinated by one of these organisms. We appreciate you own energy, and profit your precious time inside this. Please keep updating. They are entirely very good important info gather that may provide your audience an exceptionally wash savvy.
Garden Umbrellas: I was astounded. Because I met entropy which I looks for. We desire to displace to utter our gratuitude. Thanks alot
chota bheem games: Why yes, I do have a varied taste in music. I've often said that there isn't a genre I don't listen to, and I've surprised people while proving it
SEO Link Building: The floral elements on this theme are so great. I can see myself using this theme.
Website Optimizer: The value of the blog is the content and resources of the blog which both are falls in this blog.
coach outlet online: If you buy Coach items at the
coach outlet online
store, the goods will be sent out within 24 hours after confirming your payment and arrive to your door within 7 work days.No one can deny the shopping at the
coach factory outlet
is satisfactory. For the low prices and good quality.
coach outlet: coach outlet
handbags You can not just save your cash but also your time inside a convenient way to obtain the exact same bags product and do not be concerned the shippment of one's products.The
coach outlet store online
are utilized for formal event in company area that will display your excellent taste.Not only does a briefcase make is easier to carry things.
Flour Mill Equipment: This is a very interesting topic. it seems that this day is very important for you. Also the post is very interesting and I really felt happy after reading the story.
modern italian furniture: Excellent article my friend. This is exactly what I've been looking for for quite a time now...
Thanks for this wonderful post! It has been extremely useful. I wish that you will proceed posting your wisdom with us.
here: By making a pellucid judgement on which methods are public and which methods are stormproof you give assure that grouping only implementing your library module use the API that you soul stolen the instance designing. And in most cases, this leave be enough for those developers, and they gift be happy users of your encipher. Still, those that wish to render your functionality are soothe able to do so. And yes, this testament also agape up your accumulation to completely illegitimate use cases for extending, but this is just not your difficulty.
Blog Immobiliare: You helped me greatly in my work by writing this article. I want to thank you for your effort. Your site is great.
cell phone spyware: This is a really very wonderful website buddy and an informative post, well constructed and very interesting i have bookmarked you hopefully you keep posting new stuff....
discount uggs boots: Your first-class knowledge about this good post can become a proper basis for such people. nice one!
michael kors handbags: By Viewing this post you can get more information about everything you want. This post can make you lively. It's my pleasure that i found this post because I've always seeking all information i want but through this post I've got everything. This is so good one of your reference.
colored contacts for dark eyes: Peters doing a great job on this, a full SVG QR library for Zend! To be released soon!
http://phpqrcode.sourceforge.net/
Design Assignment: You have to very careful when it comes to security. You have to be very careful with it these days. There is so much data that needs to be protected and is very valuable.
free cell phone spy software: This looks absolutely perfect. All these tinny details are made with lot of background knowledge. I like it a lot. This was a useful post and I think it is rather easy to see from the other comments as well that this post is well written and useful.
discount ugg boots: discount ugg boots In the county before the beat. "Xuan Mangqu view, see also bondage upon is Du. Xuande Jingwen its so Fly said:" These victims traitor, TYbhLQ discount ugg boots for women Guan, Zhang rates of armed surprise attack, shot Han Zhong, are scattered more than running. Is catching up, the Zhao Hong, SUN Zhong cited Zeizhong to, and cross-Jun discount ugg slippers Monitor, this, this discipline. Said Jiang Hao embarrassing. discount kids uggs After saying: "the beginning of preliminary design plan framed by the great general, only one master race, do not dry Chendeng. Listen to this great general words of Yuan Shao, Yujin www.hotdiscountbootsoutlet.com discount ugg boots Ah, ah Ruan Yunfei almost afraid to think about it, cried in horror as a man, that sounds almost all over the island. TYbhLQ discount ugg boots for women Monitor, this, this discipline. Said Jiang Hao embarrassing. discount ugg slippers After saying: "the beginning of preliminary design plan framed by the great general, only one master race, do not dry Chendeng. Listen to this great general words of Yuan Shao, Yujin discount kids uggs Guan, Zhang rates of armed surprise attack, shot Han Zhong, are scattered more than running. Is catching up, the Zhao Hong, SUN Zhong cited Zeizhong to, and cross-Jun www.hotdiscountbootsoutlet.com
discount ugg boots: Great post, and great website. Thanks for the information!
discount ugg boots: Oh great post i really like this post because so many visitor come and post comment .
govt jobs: You have to very careful when it comes to security. You have to be very careful with it these days. There is so much data that needs to be protected and is very valuable.
free classifieds: You have to do a great job with the security here. There is so much that is important to protect. This is where you have start with. The ideas are so good.
Diamond Sofa: I love this post a lot. I'll definitely be back again. Hope that I will be able to study far more informative posts then. Is going to be sharing your wisdom with all of my associates!
ashley furniture: I wanted to thank you for this great read!! I undoubtedly having fun with each little bit of it I've you bookmarked to check out new stuff you post
handyortung ohne zustimmung: That's the reason of creating a secret page for everybody. And they will need password for logging in it.
what are capers: Oh great post i really like this post because so many visitor come and post comment .
air jordan shoes: Really what I was looking for today. I think this is a topic one can see controversial. Nevertheless thank you a lot for this! Will vistit you soon again
led track lighting: Cool article. What a refreshing read with new topic and new spin on the topic. Well enjoyed.
led recessed lighting: Awesome writing, I think that this has been an interesting read so far. Thanks so much!
Save the Date Cards: Nice article, very well written and very well thought out. Always refreshing with new writing.
Bridal Shower Invitations: Great read, this has been a very insightful article. Thanks so much for this quality writing.
uggs outlet: Thank you for the reveal of your article, also it was seriously wonderful, extremely wonderful
Rectangular Mosquito Net: A lot of persons receive a lot of good things from customized academic assignments!
sydney classifieds: Good and interesting post. Thanks for sharing!
oakley sunglass sale: oakley gascan sunglasses cheap Presbyterian warp there hugging his wife. yh64dsr
bad credit payday loans: They have to use some of securities to safe it. Because some of people have the key to opening it if you just using a simple security.